Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL In and Not In

Author  Topic 

Barryt82
Starting Member

1 Post

Posted - 2012-02-07 : 17:10:23
select * from candidates where candidate_id not in
(select owner_id from addresses where country in ('Ericsson','Uganda','Ghana','Kenya','Tanzania'))

select * from candidates where candidate_id in
(select owner_id from addresses where country not in ('Ericsson','Uganda','Ghana','Kenya','Tanzania'))

hi in the above code, would both give the same result? the only difference is where the 'in' and 'not' are placed. is it possible to switch them around and see the same results?

Thanks

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-07 : 17:14:49
They might.

But in most cases they wouldn't

Did you run a test yourself?

It's very easy to do



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2012-02-07 : 17:20:46
Note that if owner_id CAN be NULL then

... where candidate_id not in
(select owner_id from addresses ...

will return nothing if one/many of the values of [candidate_id] IS NULL. That will give you a very specific difference between the two! on the other hand IN will not be bothered by one/many values being NULL in the list.
Go to Top of Page
   

- Advertisement -