Powered By Blogger

Thursday 2 June 2016

Difference Between IN and EXISTS




IN
EXIST
1
IN is faster than EXISTS, when the sub-query results is very small.
EXISTS is much faster than IN, when the sub-query results is very large.
2
IN can't compare anything with null, so it has no result for null. For Example:Select *
from Table
where Table_ID in (select null)

Above one would return empty.
EXISTS can compare everything with null.
Select * 
from Table
where exists (select null).

Above query will return all the records

3
If you are using the IN operator, the SQL engine will scan all records fetched from the inner query
On the other hand if we are using EXISTS, the SQL engine will stop the scanning process as soon as it found a match.
4
 If Most of the filter criteria is in subquery then better to use IN
If most of the filter criteria is in main query then better to use EXISTS.

No comments: