Do the following two queries produce the same result?
1. SELECT * FROM Table a LEFT JOIN Table b ON a.id = b.id;
2. SELECT * FROM Table b RIGHT JOIN Table a ON a.id = b.id;
Do the following two queries produce the same result?
1. SELECT * FROM Table a LEFT JOIN Table b ON a.id = b.id;
2. SELECT * FROM Table b RIGHT JOIN Table a ON a.id = b.id;
Yes both will provide same result with different sorting order
No, the two are not the same because the columns are in different orders. You are using select *
, so the ordering of the columns is based on the order of the tables in from
clause.
That said, with two tables, left join
and right join
are equivalent, with the tables switched and the same join
conditions. This does not apply as more joins are added. So, these can be different:
select . . .
from a left join b left join c
versus:
select c right join b right join a
The reason is because of how the joins are grouped, which is always from left-to-right unless you have parentheses.
from (a left join b) left join c
select (c right join b) right join a
If you are interested in the more general situation (and perhaps examples of when they are not the same), then ask a new question. That said, my advice is to always use left join
s. In my experience, right join
is almost never needed.