Reputation: 140
This is a very simple question but i couldn't get the result for the life of me.
Where is the "leftness" or "rightness" of a table determined in SQL?
so if have to tables, t1 and t2 with this query:
SELECT * FROM t1 LEFT JOIN t2 ON t1.id = t2.id
I'm certain t1 is on the left but what if i change the order of the ON statement like so:
SELECT * FROM t1 LEFT JOIN t2 ON t2.id = t1.id
is t2 now on the left or is t1 still on the left because it is on the left of LEFT?
i appreciate the time anyone takes to answer this simple question.
n.b. my question is not about the difference between LEFT and RIGHT joins
Upvotes: 0
Views: 54
Reputation: 455
The order of a table name matters in Join (LEFT
, RIGHT
), Not the order of join condition.
So here, your both queries are same.
Upvotes: 3
Reputation: 952
The LEFT JOIN keyword returns all records from the left table (table1), and the matched records from the right table (table2). The result is NULL from the right side, if there is no match.
Similarly
The RIGHT JOIN keyword returns all records from the right table (table2), and the matched records from the left table (table1). The result is NULL from the left side, when there is no match.
Putting in your terms the LEFTness or RIGHTness of a table is determined at
FROM table1 LEFT/RIGHT JOIN table2.
Whatever table is before the keyword LEFT join is considered Left table and similarly whatever table is after keyword RIGHT join is considered as Right table. For more info you can refer SQL-Joins
Upvotes: 0
Reputation: 6524
Left join or Left outer join requires table name with the condition "t1 LEFT JOIN t2". Here, t1 is left joined with t2 meaning all elements of t1 and matching rows on t2.
Upvotes: 0