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
 left join syntax

Author  Topic 

sqlclarify
Yak Posting Veteran

56 Posts

Posted - 2009-05-11 : 02:15:46
In www.w3schools.com , the syntax for left joins is given as:

SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

But I notice some people reverse the join condition.. does that produce the same results?

SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name2.column_name=table_name1.column_name

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-05-11 : 02:24:16
quote:
Originally posted by sqlclarify

In www.w3schools.com , the syntax for left joins is given as:

SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

But I notice some people reverse the join condition.. does that produce the same results?

SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name2.column_name=table_name1.column_name


Yes

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-11 : 11:16:59
quote:
Originally posted by sqlclarify

In www.w3schools.com , the syntax for left joins is given as:

SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

But I notice some people reverse the join condition.. does that produce the same results?

SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name2.column_name=table_name1.column_name


the order in which columns are compared in on does not affect the results. The only think to be careful are which table you use on left and right side of left join.
also it would be better to use short aliases rather than repeating table name everywhere like

SELECT column_name(s)
FROM table_name1 t1
LEFT JOIN table_name2 t2
ON t1.column_name=t2.column_name


Go to Top of Page
   

- Advertisement -