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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Order of Joins

Author  Topic 

webcomber
Starting Member

10 Posts

Posted - 2008-06-02 : 11:59:22
Hi -

I have a general question about the order of joins. I have noticed that in some of the queries I am looking at, there are lots of LEFT and RIGHT Joins one after the other. My question is which table is considered the LEFT or RIGHT or original table if there are 9 or so joins after it?



For example, if a query says
select ORD.orderID, ORD.CustomerID, EMP.employeeID, SOX.AugitID
FROM Dbo.employees EMP
INNER JOIN
dbo.orders ORD
On
emp.employeeid=ORD.employeeid
LEFT OUTER JOIN
dbo.Customers CUST
ON CUST.CustomerID=ORD.CustomerID
RIGHT OUTER JOIN
SOX.Audit.ID
ON ORD.OrderID = SOX.Audit.ID

the query INNER JOINS Employee and Orders table, but for the LEFT and RIGHT joins, is the Original FROM dbo.Employees EMP always considered the original table from which a LEFT JOIN is done, and if so is the RIGHT OUTER JOIN, the SOX table, taking the JOIN on the original FROM Table FROM Dbo.employees EMP, or from the table preceding the RIGHT OUTER JOIN, the Customers table. This is a simple example, but these queries have one table after another for about 9 - 15 joins (RIGHT, LEFT, etc.). Whatever applies to the simple query above, I will take to the more complex ones.

Thanks -


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-02 : 12:02:44
quote:
Originally posted by webcomber

Hi -

I have a general question about the order of joins. I have noticed that in some of the queries I am looking at, there are lots of LEFT and RIGHT Joins one after the other. My question is which table is considered the LEFT or RIGHT or original table if there are 9 or so joins after it?



For example, if a query says
select ORD.orderID, ORD.CustomerID, EMP.employeeID, SOX.AugitID
FROM Dbo.employees EMP
INNER JOIN
dbo.orders ORD
On
emp.employeeid=ORD.employeeid
LEFT OUTER JOIN
dbo.Customers CUST
ON CUST.CustomerID=ORD.CustomerID
RIGHT OUTER JOIN
SOX.Audit.ID
ON ORD.OrderID = SOX.Audit.ID

the query INNER JOINS Employee and Orders table, but for the LEFT and RIGHT joins, is the Original FROM dbo.Employees EMP always considered the original table from which a LEFT JOIN is done, and if so is the RIGHT OUTER JOIN, the SOX table, taking the JOIN on the original FROM Table FROM Dbo.employees EMP, or from the table preceding the RIGHT OUTER JOIN, the Customers table. This is a simple example, but these queries have one table after another for about 9 - 15 joins (RIGHT, LEFT, etc.). Whatever applies to the simple query above, I will take to the more complex ones.

Thanks -





The ON condition specifies what will be the LEFT or RIGHT table. Here for Customers the left table will be Orders and for SOX.Audit left table will be orders
Go to Top of Page

webcomber
Starting Member

10 Posts

Posted - 2008-06-02 : 13:08:42
Hello -

I thought the JOIN statement always determined which was the LEFT or RIGHT table. SO I guess in this case, the LEFT table is the result of the first query, and the RIGHT table, Audit, is JOINED to the LEFT JOIN results. I suppose in these cases, we have to track the results of the query for each join.
I am not sure if the ON statement determines any order ??
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-02 : 13:11:50
quote:
Originally posted by webcomber

Hello -

I thought the JOIN statement always determined which was the LEFT or RIGHT table. SO I guess in this case, the LEFT table is the result of the first query, and the RIGHT table, Audit, is JOINED to the LEFT JOIN results. I suppose in these cases, we have to track the results of the query for each join.
I am not sure if the ON statement determines any order ??


Exactly but based on the columns specified in ON clause
Nope. the ON conatins matching condition based on ORD column which is Orders table. So it will take Orders as left table.
Go to Top of Page

ibeckett
Starting Member

12 Posts

Posted - 2008-06-02 : 15:23:24
[code]select ORD.orderID, ORD.CustomerID, EMP.employeeID, SOX.AugitID
FROM Dbo.employees EMP -- this table is on the left
JOIN dbo.orders ORD On emp.employeeid=ORD.employeeid -- this table is on the left
LEFT JOIN dbo.Customers CUST ON CUST.CustomerID=ORD.CustomerID -- this table is on the right
RIGHT JOIN SOX.Audit.ID ON ORD.OrderID = SOX.Audit.ID -- this table is on the right[/code]

Ian Beckett
ibeckett at gmail dot com
www.sqlblog.ibeckett.com
Go to Top of Page

cat_jesus
Aged Yak Warrior

547 Posts

Posted - 2008-06-02 : 15:51:17
I think it's best to pick either left or right join notation for outer joins and stick with that.

In other words I never use right join, I always use left.




An infinite universe is the ultimate cartesian product.
Go to Top of Page

webcomber
Starting Member

10 Posts

Posted - 2008-06-02 : 22:49:01
So it looks like the result of the first INNER JOIN is the LEFT table for the LEFT OUTER JOIN, and the result of that join is the left table of the next join.
Go to Top of Page

webcomber
Starting Member

10 Posts

Posted - 2008-06-02 : 22:55:38

[/quote]
The ON condition specifies what will be the LEFT or RIGHT table. Here for Customers the left table will be Orders and for SOX.Audit left table will be orders
[/quote]

But if I say:

LEFT OUTER JOIN
dbo.Customers CUST
ON ORD.CustomerID = CUST.CustomerID
RIGHT OUTER JOIN
SOX.Audit.ID
ON SOX.Audit.ID = ORD.OrderID

dbo.Customers is still the RIGHT table in the LEFT OUTER JOIN, and SOX is still the RIGHT table on the RIGHT OUTER JOIN, even though I flipped the ON clauses around.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-03 : 00:07:56
quote:
Originally posted by webcomber




The ON condition specifies what will be the LEFT or RIGHT table. Here for Customers the left table will be Orders and for SOX.Audit left table will be orders
[/quote]

But if I say:

LEFT OUTER JOIN
dbo.Customers CUST
ON ORD.CustomerID = CUST.CustomerIDRIGHT OUTER JOIN
SOX.Audit.ID
ON SOX.Audit.ID = ORD.OrderID

dbo.Customers is still the RIGHT table in the LEFT OUTER JOIN, and SOX is still the RIGHT table on the RIGHT OUTER JOIN, even though I flipped the ON clauses around.
[/quote]
Look at ON condition for LEFT JOIN its
ON ORD.CustomerID = CUST.CustomerID
so tables involved are ORD (dbo.orders) AND CUST (dbo.Customers )
Now look at join

(left part)
LEFT OUTER JOIN
dbo.Customers CUST

so dbo.Customers is the right table. The left table is other one involved which is dbo.orders

Go to Top of Page
   

- Advertisement -