If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Difference between EXISTS and a join

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-06-11, 07:28
Breako Breako is offline
Registered User
 
Join Date: Jan 2006
Posts: 119
Difference between EXISTS and a join

Hi,
The EXIST predicate is always followed by a subselect.

For example the get the rows for values in department which have deptno which matches a workdept in the employee table you'd do:

SELECT deptno, deptname FROM department WHERE EXISTS (SELECT workdept FROM employee WHERE workdept = dept)


This can also be done by just using a join.

SELECT department.deptno, department.deptname FROM department, employee WHERE department.deptno = employee.workdept;

What's the pro's and con's of both approaches?

Thanks
Reply With Quote
  #2 (permalink)  
Old 07-06-11, 08:33
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
You can try yourself.

Second query(using a join) will return multiple rows for a department.
Exact number of rows for a department will be the number of employees belonging to the department.

If you want to get same result as first query, you should add DISTINCT keyword for second query.
Reply With Quote
  #3 (permalink)  
Old 07-06-11, 08:40
Breako Breako is offline
Registered User
 
Join Date: Jan 2006
Posts: 119
Quote:
Originally Posted by tonkuma View Post
You can try yourself.

Second query(using a join) will return multiple rows for a department.
Exact number of rows for a department will be the number of employees belonging to the department.

If you want to get same result as first query, you should add DISTINCT keyword for second query.
Ok so same question with DISTINCT included?

Thanks.
Reply With Quote
  #4 (permalink)  
Old 07-06-11, 08:52
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
I want to say samethings as Marcus A in this thread
Using equals or IN
Quote:
Do an Explain and find out.
Reply With Quote
  #5 (permalink)  
Old 07-06-11, 13:59
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
A good rule-of-thumb, in general, is to try putting into subqueries (with EXISTS or IN) all tables from which no direct information (i.e.: column values or counts) are needed.

This will most often be much more readable that the JOIN, and most often also more performant. Beware: a join could be more performant than an EXISTS. If it really matters: use EXPLAIN to find out, and document the reason for your choice (using "--" comments in SQL).
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On