Getting Started with Adobe After Effects - Part 6: Motion Blur
A collection of quick technology learning tips from what people around you learn every day

Importance of the order of ON clause when LEFT and INNER JOINs are involved

Aug 12 2011 10:35PM by Naomi   

Recently in MSDN forums, OP wanted to get all results from the first table (called it r) LEFT JOINed with the other table (m) and INNER JOINed with another table (called s). In my mind it was a tricky question and I solved it by a separate CTE first doing the INNER JOIN between s and m and then LEFT JOINING with the result of the r table.

However, user cyberkiwi showed a different approach that I was never aware of (as I always write JOIN conditions right after JOIN keyword):

<pre class="brush: plain">
declare @A table (id int identity, a int)
insert @A select 1 union all select 2 union all select 3
declare @B table (id int identity, b int)
insert @B select 2 union all select 3
declare @C table (id int identity, c int)
insert @C select 3 union all select 4

select *
from @A A
left join
    	@B B
    	inner join @C C
    		on B.b = C.c
    on A.A = B.B

-- output
--id,a,id,b,id,c
--1,1,NULL,NULL,NULL,NULL
--2,2,NULL,NULL,NULL,NULL
--3,3,2,3,1,3

-- The version I write --
select *
from @A A
left join
    	@B B on A.A = B.B
    	inner join @C C
    		on B.b = C.c
</pre>

Do you see the difference between the first way and the second (most common way, for me, at least)? Well, the first way is the solution for the OP's problem. You can examine the execution plan to see, that it does the same as CTE version does: first INNER joins b & c and then r LEFT JOINed with that result.

Hope this was helpful for you as well!

Read More..   [5 clicks]

Published under: SQL Server Tips ·  ·  ·  · 


Naomi
33 · 6% · 1774
10
 
2
 
 
0
Incorrect
 
0
Interesting
 
0
Forgotten



Submit

4  Comments  

  • Good one Naomi, We can use below query also. Probably cyberkiwi's solution is shorthand solution for below one...

    select *
    from @A A
    left join
        (select B.id,B.b,C.c from	@B B
        	inner join @C C
        		on B.b = C.c)B1
        on A.A = B1.B
    
    commented on Aug 12 2011 10:31PM
    Ramireddy
    2 · 41% · 12972
  • cyberkiwi is a school, I always learn from his answers in EE !!

    commented on Aug 15 2011 9:44AM
    Jason Yousef
    152 · 1% · 319
  • It's pity that one can't use brackets to make it more readable. Anyway, good to know this, thanks!

    commented on Aug 16 2011 1:37AM
    Adam Tokarski
    58 · 3% · 1021
  • you can use brackets:

    select * from @A A left join (@B B  inner join @C C on B.b = C.c) on A.A = B.B
    

    I think indentation can be more important than brackets to make SQL code readable, though...

    commented on Aug 30 2011 9:53AM
    paul.ramster
    554 · 0% · 66

Your Comment


Sign Up or Login to post a comment.

"Importance of the order of ON clause when LEFT and INNER JOINs are involved" rated 5 out of 5 by 10 readers
Importance of the order of ON clause when LEFT and INNER JOINs are involved , 5.0 out of 5 based on 10 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]