Difference between IN and EXISTS

Answer Posted / subathra

IN: Inner query executes first and drives the outer query.
EXISTS: Outer query executes first and compares tge rows
retrived with the inner query.
Consider table tab1 has 1000 rows and table tab2 has 1000
rows.
IN: select t1.empid from tab1 t1 where t1.code in (select
t2.code from tab2 t2) -- All rows in t1 will read with t2
and the effect is 1000 X 1000 rows.
EXISTS: select t1.empid from tab1 t1 where exists (select 1
from tab2 t2 where t1.code=t2.code) -- Max of 1 row will be
read for each row of t1 and thus reduces the processing
overhead.
Thumb rule:
1) If the majority of the filtering are in the sub query
then use IN.
1) If the majority of the filtering are in the outer query
then use EXISTS.

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain the methods used to protect source code of pl/sql.

542


What is %type in sql?

510


What is the maximum rows in csv?

485


What is the default isolation level in sql server? : Transact sql

564


What does pl sql developer do?

507






What is parameter substitution in sql?

524


Is delete faster than truncate?

545


Why truncate is used in sql?

527


Is primary key an index?

534


What are the sql commands?

606


What are the advantages of sql? Explain

648


Why does sql need a server?

562


What is cost in sql execution plan?

489


What are the events on which a database trigger can be based?

594


what is the difference between primary key and unique key? : Sql dba

527