lwadwell,
sorry ... cut and paste mistakes ...
SELECT * FROM table1 t1 WHERE col1 IN (select val FROM table2)
could be written as
SELECT * FROM table1 t1 WHERE EXISTS (select val FROM table2 t2 WHERE t1.col1 = t2.val)
Here they are very similar.
Now take this example ...
SELECT * FROM table1 WHERE EXISTS (select val FROM table2 t2 WHERE t1.col1 > t2.val)
as there is no guarentee of equality ... an IN may not work.
lwadwell
Main Topics
Browse All Topics





by: lwadwellPosted on 2009-07-01 at 19:47:49ID: 24760064
Hi waranagal,
The "exists" and the "in" clauses can be very similar - and in fact nearly the same, but not always.
For example ...
SELECT * FROM table1 t1 WHERE col1 IN (select val FROM table2)
could be written as
SELECT * FROM table1 t1 WHERE EXISTS (select val FROM table2 t1.col1 = t1.val)
Here they are very similar.
Now take this example ...
SELECT * FROM table1 WHERE EXISTS (select val FROM table2 t1.col1 > t1.val)
as there is no guarentee of equality ... an IN may not work.
Also, from a execution point of view - they can work differently and where both can be used, depending on indexes, data set sizes etc. - one can offer better performance than the other.
Is this clear??
lwadwell