Solved

SQL Exists function help

Posted on 2007-11-30
7
1,070 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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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 46

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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.​
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

863 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

22 Experts available now in Live!

Get 1:1 Help Now