Solved

SQL Exists function help

Posted on 2007-11-30
7
1,068 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
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 45

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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.​
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now