dbTalk Databases Forums  

Non-Relating Join Criteria

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Non-Relating Join Criteria in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Fred.
 
Posts: n/a

Default Non-Relating Join Criteria - 11-02-2011 , 07:53 AM






Transact SQL apparently supports one-sided critera,
such as the "a.SEL=15" in (1) below. in the join
expression. I would expect that both (1) and (2)
would return the same result set, and the examples
I have tried match this expectation.

My question is whether or not there can be a strong
reason to prefer one over the other, such as impact
on the order of evaluation.

CREATE TABLE a (APK int PRIMARY KEY, SEL int, ... )
CREATE TABLE b (BPK int PRIMARY KEY, AFK int, ... )

(1) SELECT ... FROM a INNER JOIN b
ON a.APK=b.AFK AND a.SEL=15

(2) SELECT ... FROM a INNER JOIN b
ON a.APK=b.AFK
WHERE a.SEL=15

Fred.

Reply With Quote
  #2  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: Non-Relating Join Criteria - 11-02-2011 , 08:08 AM






Hi Fred,

Quote:
My question is whether or not there can be a strong
reason to prefer one over the other, such as impact
on the order of evaluation.
No. Order of evaluation can and will be shuffled by the optimizer if
that can improve performance.

The only reason for using one and not the other is readability of the
query.

Note, though, that for OUTER JOINs, the two queries are NOT
equivalent!
--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis

Reply With Quote
  #3  
Old   
Bob Barrows
 
Posts: n/a

Default Re: Non-Relating Join Criteria - 11-02-2011 , 08:10 AM



Fred. wrote:
Quote:
Transact SQL apparently supports one-sided critera,
such as the "a.SEL=15" in (1) below. in the join
expression. I would expect that both (1) and (2)
would return the same result set, and the examples
I have tried match this expectation.

My question is whether or not there can be a strong
reason to prefer one over the other, such as impact
on the order of evaluation.

CREATE TABLE a (APK int PRIMARY KEY, SEL int, ... )
CREATE TABLE b (BPK int PRIMARY KEY, AFK int, ... )

(1) SELECT ... FROM a INNER JOIN b
ON a.APK=b.AFK AND a.SEL=15

(2) SELECT ... FROM a INNER JOIN b
ON a.APK=b.AFK
WHERE a.SEL=15

There is a nice article about this in BOL (Books Online - it's installed
when you install the client tools).
The short story is:
with inner joins there is no difference - the same query execution plan is
used for both variations.

With outer joins, however, different results can be obtained depending on
where you put the criterion, especially when the predicate applies to the
table on the right side of a left outer join:

(1) SELECT ... FROM b LEFT JOIN a
ON a.APK=b.AFK AND a.SEL=15

(2) SELECT ... FROM b LEFT JOIN a
ON a.APK=b.AFK
WHERE a.SEL=15

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.