Solved

SQL Exists function help

Posted on 2007-11-30
7
1,074 Views
Last Modified: 2012-08-14
Why does code 1 below give different results than code 2? Which one should I trust?

CODE 1
SELECT   * FROM   [User]
WHERE   NOT EXISTS (SELECT     userid FROM [user_location])
GIVES NO RESULTS

CODE 2
SELECT   * FROM   [User]
WHERE   UserId NOT IN (SELECT  userid FROM [user_location])
GIVES SOME RESULTS
0
Comment
Question by:mattphung
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 20383303
Hello mattphung,

Code 2  or this one

SELECT   * FROM   [User] u
WHERE   NOT EXISTS (SELECT     userid FROM [user_location] ul where u.userID = ul.UserID )



Aneesh R
0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20383314
only this syntax is correct

SELECT   * FROM   [User]
WHERE   UserId NOT IN (SELECT  userid FROM [user_location])
0
 
LVL 6

Expert Comment

by:messen1975
ID: 20383316
I'd expect Code 1 to return no results
and
Code 2 to return all rows where the UserID is not in user_location
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20383320
For this
SELECT   * FROM   [User]
WHERE  (Which field you are checking is missing here )  NOT EXISTS (SELECT     userid FROM [user_location])
GIVES NO RESULTS
0
 
LVL 6

Expert Comment

by:messen1975
ID: 20383338
I'd right that (Assuming you want all Users Not in user_location) as


Select * 
From User left Join User_Location on user.UserID = user_location.userid
Where user_location.userid is null

Open in new window

0
 
LVL 49

Expert Comment

by:Vitor Montalvão
ID: 20383348
Code 1 doesn't return results because it's incomplete. Need to join user_location to user, like aneeshattingal wrote. So you should trust Code 2.
0
 

Author Comment

by:mattphung
ID: 20383368
shasunder:
 Your code gave a syntax error.  Thanks anywhere.

aneeshattingal: solution was the best one
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Table where row act as column 11 70
find SQL job run average duration 24 57
Convert VBA UDF to SQl SERVER UDF 4 50
SQL - Ordering Supervisor Hierarchy 2 13
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question