SELECT * FROM FAMILY_DETAILS WHERE EXISTS(SELECT EMPLOYEE_ID FROM EMPLOYEE_MASTER WHERE EMPLOYEE_ID='EMP001')
SELECT * FROM FAMILY_DETAILS WHERE EMPLOYEE_ID IN(SELECT EMPLOYEE_ID FROM EMPLOYEE_MASTER WHERE EMPLOYEE_ID='EMP001')
I have these two queries in sql server 2008 R2. Both will give same results. Actually from these above queries output is only two rows, But in case if i have large number of records in that scenario Which one is better to use? and why?
Priyaranjan K SPosted Oct 30, 2015, 2:50 AM
Hi ,
The guideline of the MS SQL query performance says that if we needs "IN" clause, instead of using "IN" clause we must use the "EXISTS" clause because "EXISTS" clause improve the performance of the query.
IN Clause
Returns true if a specified value matches any value in a sub query or a list.
EXISTS Clause
Returns true if a sub query contains any rows.
Ref:
http://sqlknowledgebank.blogspot.com/2012/11/in-exists-clause-and-their-performance.html
http://weblogs.sqlteam.com/mladenp/archive/2007/05/18/60210.aspx
Thanks,
If you found this useful Please Accept this as Answer.
Jignesh TrivediPosted Oct 30, 2015, 2:18 AM
Ravi PatelPosted Oct 30, 2015, 1:50 AM