Search code examples
sqljoinleft-joinright-join

Right join vs left join, which table is left vs right?


Given

select * 
from a
left join b
on a.id = b.id

is table a left and table b right?

Would that be equivalent to

Select *
from a
right join b
on b.id = a.id

because I switched left and right while flipping the ON clause? Or is a still left because it came first and b is right because it's the thing we're joining?

Thank you.


Solution

  • No. "left" and "right" refer to the ordering of the tables in the FROM clause. So these are equivalent:

    select * 
    from a left join
         b
         on a.id = b.id
    
    select * 
    from b right join
         a
         on a.id = b.id
    

    These two on clauses do exactly the same thing:

    on a.id = b.id
    on b.id = a.id
    

    They do not affect the results at all.