Solved

not in  to  not exists

Posted on 2013-06-18
2
156 Views
Last Modified: 2013-06-21
hi guys

I am trying to convert my NOT IN sql to a NOT EXISTS sql

NOT IN..
select *  FROM table2 where ExecutionId = '966'
and StaffID NOT IN (select BookId from table1 TE
where TE.ExecutionId = '966')

20433 rows


NOT EXISTS..
select * from table2 where
ExecutionId = '966' and  NOT EXISTS (select 1 from table1  
where table1.BookId  = table2.StaffID)            

20455 rows

I was expecting same results, any idea where my NOT EXISTS sql is getting me wrong
result.

thanks
0
Comment
Question by:royjayd
2 Comments
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39257125
The inner join will result in more rows if there are duplicates. The IN eliminates duplicated StaffIDs. Also it is not guranteed that removing the ExecutionId restriction from table1 will not change the results.
0
 

Author Comment

by:royjayd
ID: 39265949
Qlemo,
i found out the answer is NOT EXISTS takes Null values into consideration and NOT IN
just ignores them.
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
query help 18 52
Grid querry results 41 56
SQL Server R2 Stored procedure performance 8 33
SqlAdvisor 2016 3 11
Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

13 Experts available now in Live!

Get 1:1 Help Now