SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 JOIN LOGIC
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

bigeldm
Starting Member

USA
2 Posts

Posted - 01/31/2013 :  17:39:31  Show Profile  Reply with Quote
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.

Edited by - bigeldm on 01/31/2013 17:43:16

jimf
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 01/31/2013 :  18:42:51  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 02/01/2013 :  02:38:35  Show Profile  Reply with Quote
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

USA
2 Posts

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

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 02/01/2013 :  12:22:51  Show Profile  Reply with Quote
welcome

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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.12 seconds. Powered By: Snitz Forums 2000