Skip to content
Advertisement

Is RIGHT JOIN Ever Required?

Do any queries exist that require RIGHT JOIN, or can they always be re-written with LEFT JOIN?

And more specifically, how do you re-write this one without the right join (and I guess implicitly without any subqueries or other fanciness):

SELECT *
FROM t1
LEFT JOIN t2 ON t1.k2 = t2.k2
RIGHT JOIN t3 ON t3.k3 = t2.k3

Advertisement

Answer

You can always use only left Joins…

SELECT * FROM t1
    LEFT JOIN t2 ON t1.k2 = t2.k2
    RIGHT JOIN t3 ON t3.k3 = t2.k3

is equivilent to:

Select * From t3 
   Left Join (t1 Left Join t2 
                  On t2.k2 = t1.k2)
      On T2.k3 = T3.K3

In general I always try to use only Left Joins, as the table on the left in a Left Join is the one whose rows are ALL included in the output, and I like to think of it, (The Left side) as the “base” set I am performing the cartesion product (join) against … So I like to have it first in the SQL…

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement