SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 not in
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

arkiboys
Flowing Fount of Yak Knowledge

1406 Posts

Posted - 05/14/2013 :  04:08:13  Show Profile  Reply with Quote
Hello,
in sql instead of using:
select * from tbl1
where CustID not in (select CustID from tbl2)

Questions:
1- Can I use join query instead of not in ?
2- if so, then is that more efficient than not in ?

Thanks

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 05/14/2013 :  04:13:07  Show Profile  Reply with Quote
1. You can. you need to use left join and filter on NULL

like

SELECT t1.*
FROM tbl1 t1
LEFT JOIN tbl2 t2
On t2.CustID = t1.CustID
WHERE t2.CustID IS NULL

Please keep in mind that if the relationship between tables are not one to one you may get tbl1 data duplicated in output and hence have to use DISTINCT then

2.Depends on factors like table size,indexes presnt etc. Though mostly I've seen NOT EXISTS and JOIN performing better than NOT IN

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

arkiboys
Flowing Fount of Yak Knowledge

1406 Posts

Posted - 05/14/2013 :  05:16:37  Show Profile  Reply with Quote
Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 05/14/2013 :  05:27:17  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

djj55
Constraint Violating Yak Guru

USA
312 Posts

Posted - 05/14/2013 :  11:28:04  Show Profile  Reply with Quote
I found this was helpful: http://explainextended.com/2009/09/15/not-in-vs-not-exists-vs-left-join-is-null-sql-server/

djj
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.1 seconds. Powered By: Snitz Forums 2000