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 > Data Access, Manipulation & Batch Languages > ANSI SQL > exists and in clause

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-02-06, 04:19
sangroid sangroid is offline
Registered User
 
Join Date: Oct 2006
Posts: 1
exists and in clause

Hi all,
I am confused where to use "exists" and where to use "in" clause...they sound similar...i am totally confused..will you please make me clear ?

Thank you
Reply With Quote
  #2 (permalink)  
Old 10-02-06, 04:36
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
use EXISTS when you need to ensure something exists

use IN when you need something to be in a list of things

examples

SELECT name FROM students
WHERE EXISTS (SELECT * FROM awards WHERE name = students.name)

SELECT name FROM students
WHERE teacher IN ( 'Smith','Jones' )
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-02-06, 08:39
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
As a rule of thumb, the subquery of an "IN" will almost never be correlated, while the subquery for an "EXISTS" will always be correlated.
"Correlation" means that the subquery contains a condition that refers to (one of the) tables in the outer query.
__________________
--_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
  #4 (permalink)  
Old 10-02-06, 09:00
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
EXISTS does not always have to be correlated

SELECT apology FROM excuses WHERE sincerity='high'
AND EXISTS (SELECT game FROM nfl WHERE gamedate=current_date)

i.e. i can't talk to you tonight if there's a game on
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 10-02-06, 09:51
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Agreed. But that would be an "atypical" case. That's why I'm calling it just a "rule of thumb"; the exceptions prove the rule...
__________________
--_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
  #6 (permalink)  
Old 10-02-06, 09:53
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
ah, i see

so when you say "always", you really mean "always, except when it isn't"
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 10-02-06, 09:55
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by r937
when you say "always", you really mean "always, except when it isn't"
Let's say that I mean "always, unless you are 100% aware of what you are doing and why you're doing it that way and not differently".
__________________
--_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
  #8 (permalink)  
Old 10-02-06, 09:59
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Quote:
Originally Posted by r937
SELECT apology FROM excuses WHERE sincerity='high'
AND EXISTS (SELECT game FROM nfl WHERE gamedate=current_date)
Good lord - a single game takes all day?
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #9 (permalink)  
Old 10-02-06, 14:03
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
no, you're thinking of sticket, um, wicket, er, ah, picket, um, oh yeah, cricket

"a hunnert and fifty two over and under with nobody out and a man on third after the fifth day of the test"

nfl games are scheduled on a single day only
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 10-05-06, 08:54
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
Quote:
As a rule of thumb, the subquery of an "IN" will almost never be correlated, while the subquery for an "EXISTS" will always be correlated.
"Correlation" means that the subquery contains a condition that refers to (one of the) tables in the outer query.
Anyway correlation means that the subquery will be executed for every produced row in the main query, where with 'IN' first the subquery wil be executed once and after that the main query... Can make a big difference in performance if both querytypes are interchangeable. (And to my knowledge is the EXISTS construct designed for the subquery to contain a reference to a column in the main query. If it can evaluate to TRUE without this reference doesn't imply something meaningful will be produced)

Regards
Reply With Quote
  #11 (permalink)  
Old 10-05-06, 08:59
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
actually "subquery will be executed for every row in the main query" is a popular misconception

subqueries are usually executed as joins

but you are right, a non-correlated subquery can be evaluated once only, so it is much more efficient

however, please do not suggest that a non-correlated subquery isn't meaningful
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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