| 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.AugitIDFROM Dbo.employees EMPINNER JOINdbo.orders ORDOn emp.employeeid=ORD.employeeidLEFT OUTER JOINdbo.Customers CUSTON CUST.CustomerID=ORD.CustomerIDRIGHT OUTER JOINSOX.Audit.IDON ORD.OrderID = SOX.Audit.IDthe 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.AugitIDFROM Dbo.employees EMPINNER JOINdbo.orders ORDOn emp.employeeid=ORD.employeeidLEFT OUTER JOINdbo.Customers CUSTON CUST.CustomerID=ORD.CustomerIDRIGHT OUTER JOINSOX.Audit.IDON ORD.OrderID = SOX.Audit.IDthe 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 |
 |
|
|
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 ?? |
 |
|
|
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 clauseNope. the ON conatins matching condition based on ORD column which is Orders table. So it will take Orders as left table. |
 |
|
|
ibeckett
Starting Member
12 Posts |
Posted - 2008-06-02 : 15:23:24
|
| [code]select ORD.orderID, ORD.CustomerID, EMP.employeeID, SOX.AugitIDFROM 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 Beckettibeckett at gmail dot comwww.sqlblog.ibeckett.com |
 |
|
|
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. |
 |
|
|
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. |
 |
|
|
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 JOINdbo.Customers CUSTON ORD.CustomerID = CUST.CustomerIDRIGHT OUTER JOINSOX.Audit.IDON SOX.Audit.ID = ORD.OrderIDdbo.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. |
 |
|
|
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 JOINdbo.Customers CUSTON ORD.CustomerID = CUST.CustomerIDRIGHT OUTER JOINSOX.Audit.IDON SOX.Audit.ID = ORD.OrderIDdbo.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 itsON ORD.CustomerID = CUST.CustomerID so tables involved are ORD (dbo.orders) AND CUST (dbo.Customers )Now look at join (left part)LEFT OUTER JOINdbo.Customers CUSTso dbo.Customers is the right table. The left table is other one involved which is dbo.orders |
 |
|
|
|