Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 JOIN LOGIC

Author  Topic 

bigeldm
Starting Member

2 Posts

Posted - 2013-01-31 : 17:39:31
I can't figure out when to put the selection criteria as part of the join and when it should be part of the where clause.

for example - what is the difference between

Select table1.field1, table2.field2, table3.field3
from table1
Left Join table2
on table1.field1 = table2.field1 and table2.field2 <> 'abc'
Left Join table3
on table1.field1 = table3.field1

AND

Select table1.field1, table2.field2, table3.field3
from table1
Left Join table2
on table1.field1 = table2.field1
Left Join table3
on table1.field1 = table3.field1
WHERE table2.field2 <> 'abc' or table2.field1 is null

How does one know when to use one over the other?? I get different results depending on where I put the selection criteria and don't understand why.

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2013-01-31 : 18:42:51
Here is a really good article. You'll have to sign up to read it, but it's a free site, just like this one.
http://www.sqlservercentral.com/articles/T-SQL/93039/

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-01 : 02:38:35
see this article. you dont have to signup for this

http://weblogs.sqlteam.com/jeffs/archive/2007/05/14/criteria-on-outer-joined-tables.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bigeldm
Starting Member

2 Posts

Posted - 2013-02-01 : 08:17:29
Thank you so much - this completely answered my question.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-01 : 12:22:51
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -