| |
|
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.
|
 |

10-02-06, 04:19
|
|
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
|
|

10-02-06, 04:36
|
|
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' )
|
|

10-02-06, 08:39
|
|
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/
|
|

10-02-06, 09:00
|
|
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 
|
|

10-02-06, 09:51
|
|
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/
|
|

10-02-06, 09:53
|
|
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" 
|
|

10-02-06, 09:55
|
|
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/
|
|

10-02-06, 09:59
|
|
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.
|
|
|

10-02-06, 14:03
|
|
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
|
|

10-05-06, 08:54
|
|
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
|
|

10-05-06, 08:59
|
|
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 
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|