Interview Questions   Tutorials   Discussions   Programs   

DB2 - What is the difference between IN subselects and EXISTS subselect?




284
views
asked mar August 21, 2014 12:17 PM  

What is the difference between IN subselects and EXISTS subselect?


           

1 Answers



 
answered By Mswami   0  

The "EXISTS" keyword can be used in that way, but really it's intended as a way to avoid counting:

--this statement needs to check the entire table
select count(*) from [table] where ...

--this statement is true as soon as one match is found
exists ( select * from [table] where ... )


This is most useful where you have if conditional statements, as exists can be a lot quicker than count.

The "IN" is best used where you have a static list to pass:

 
Select * from [table] where [field] in (1, 2, 3)

When you have a table in an in statement it makes more sense to use a join, but it doesn't really matter. The query optimizer will return the same plan either way.
flag   
   add comment

Your answer

Join with account you already have

FF

Preview


Ready to start your tutorial with us? That's great! Send us an email and we will get back to you as soon as possible!

Alert