dbTalk Databases Forums  

When are subselects equivalent to joins?

comp.databases comp.databases


Discuss When are subselects equivalent to joins? in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Jeff Calico
 
Posts: n/a

Default When are subselects equivalent to joins? - 08-08-2006 , 05:32 PM






Hello everyone

I'm sure this is an old question, but I can't find a good explanation.
I've read that many, but not all, SQL queries with a subselect are in
fact equivalent to a query doing a join of the relevant tables with a
join condition.

What is an easy way for me to see this equivalence? And how do I know
when I can't use a join and *have* to use a sub-select?

thanks,
Jeff


Reply With Quote
  #2  
Old   
Tonkuma
 
Posts: n/a

Default Re: When are subselects equivalent to joins? - 08-12-2006 , 12:42 PM






Jeff Calico wrote:
Quote:
Hello everyone

I'm sure this is an old question, but I can't find a good explanation.
I've read that many, but not all, SQL queries with a subselect are in
fact equivalent to a query doing a join of the relevant tables with a
join condition.

What is an easy way for me to see this equivalence? And how do I know
when I can't use a join and *have* to use a sub-select?

thanks,
Jeff
subselect at where?
You can write subselect in many place.
For example:
1) FROM ... <table> JOIN (subselect) ...
2) FROM ... <table> JOIN LATERAL (subselect) ...
3) WHERE ... <expression> = (subselect) ...
4) WHERE ... <expression> IN (subselect) ...
5) WHERE ... <expression> BETWEEN (subselect) AND (subselect) ...
6) WHERE ... EXISTS (subselect) ...
7) SELECT ... , (subselect), ...
perhaps more.

Or, did I misunderstand exsact definition of subselect?



Reply With Quote
  #3  
Old   
Bill Karwin
 
Posts: n/a

Default Re: When are subselects equivalent to joins? - 08-12-2006 , 02:04 PM



Tonkuma wrote:
Quote:
subselect at where?
You can write subselect in many place.
Another thing to consider is that there are correlated subqueries and
noncorrelated subqueries.

Correlated means the subquery returns a different result based on the
row of the superquery, e.g.:

SELECT * FROM foo
WHERE foo.col1 IN (SELECT blah FROM bar WHERE bar.col2 = foo.col2)

Noncorrelated means the subquery is invariant on all rows of the
superquery, e.g.:

SELECT * FROM foo
WHERE foo.col1 IN (SELECT blah FROM bar WHERE bar.col2 = 42)

I can't think of a concise way to define when subqueries can be
rewritten as joins. There are some cases that are well-known, like the
noncorrelated case above. But trying to write a guide to cover _all_
these cases is really difficult.

Regards,
Bill K.


Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.