Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does anyone use Right Outer Joins?

Tags:

sql

I use INNER JOIN and LEFT OUTER JOINs all the time. However, I never seem to need RIGHT OUTER JOINs, ever.

I've seen plenty of nasty auto-generated SQL that uses right joins, but to me, that code is impossible to get my head around. I always need to rewrite it using inner and left joins to make heads or tails of it.

Does anyone actually write queries using Right joins?

like image 555
KM. Avatar asked Mar 27 '09 14:03

KM.


People also ask

Does anyone ever use right joins?

Wikipedia states: "In practice, explicit right outer joins are rarely used, since they can always be replaced with left outer joins and provide no additional functionality."

Is there a right outer join?

A RIGHT OUTER JOIN is one of the JOIN operations that allow you to specify a JOIN clause. It preserves the unmatched rows from the second (right) table, joining them with a NULL in the shape of the first (left) table. A LEFT OUTER JOIN B is equivalent to B RIGHT OUTER JOIN A, with the columns in a different order.

Which is better left join or right join?

The most substantial difference between the left and right outer join lies in the unmatched records that are obtained besides matched records. The left join takes all matching records and unmatched records of the left table while the right join takes all matching records and unmatched records of the right table.

What is the use of right outer join in SQL?

A right outer join is a method of combining tables. The result includes unmatched rows from only the table that is specified after the RIGHT OUTER JOIN clause. If you are joining two tables and want the result set to include unmatched rows from only one table, use a LEFT OUTER JOIN clause or a RIGHT OUTER JOIN clause.


1 Answers

To give one example where a RIGHT JOIN may be useful.

Suppose that there are three tables for People, Pets, and Pet Accessories. People may optionally have pets and these pets may optionally have accessories

CREATE TABLE Persons   (      PersonName VARCHAR(10) PRIMARY KEY   );  INSERT INTO Persons VALUES      ('Alice'),             ('Bob'),             ('Charles');  CREATE TABLE Pets   (      PetName    VARCHAR(10) PRIMARY KEY,      PersonName VARCHAR(10)   );  INSERT INTO Pets VALUES      ('Rover',              'Alice'),             ('Lassie',              'Alice'),             ('Fifi',              'Charles');  CREATE TABLE PetAccessories   (      AccessoryName VARCHAR(10) PRIMARY KEY,      PetName       VARCHAR(10)   );  INSERT INTO PetAccessories VALUES      ('Ball', 'Rover'),             ('Bone', 'Rover'),             ('Mouse','Fifi'); 

If the requirement is to get a result listing all people irrespective of whether or not they own a pet and information about any pets they own that also have accessories.

This doesn't work (Excludes Bob)

SELECT P.PersonName,        Pt.PetName,        Pa.AccessoryName FROM   Persons P        LEFT JOIN Pets Pt          ON P.PersonName = Pt.PersonName        INNER JOIN PetAccessories Pa          ON Pt.PetName = Pa.PetName;  

This doesn't work (Includes Lassie)

SELECT P.PersonName,        Pt.PetName,        Pa.AccessoryName FROM   Persons P        LEFT JOIN Pets Pt          ON P.PersonName = Pt.PersonName        LEFT JOIN PetAccessories Pa          ON Pt.PetName = Pa.PetName;  

This does work (but the syntax is much less commonly understood as it requires two ON clauses in succession to achieve the desired logical join order)

SELECT P.PersonName,        Pt.PetName,        Pa.AccessoryName FROM   Persons P        LEFT JOIN Pets Pt                   INNER JOIN PetAccessories Pa                     ON Pt.PetName = Pa.PetName          ON P.PersonName = Pt.PersonName; 

All in all probably easiest to use a RIGHT JOIN

SELECT P.PersonName,        Pt.PetName,        Pa.AccessoryName FROM   Pets Pt        JOIN PetAccessories Pa          ON Pt.PetName = Pa.PetName        RIGHT JOIN Persons P          ON P.PersonName = Pt.PersonName; 

Though if determined to avoid this another option would be to introduce a derived table that can be left joined to

SELECT P.PersonName,        T.PetName,        T.AccessoryName FROM   Persons P        LEFT JOIN (SELECT Pt.PetName,                          Pa.AccessoryName,                          Pt.PersonName                   FROM   Pets Pt                          JOIN PetAccessories Pa                            ON Pt.PetName = Pa.PetName) T          ON T.PersonName = P.PersonName;  

SQL Fiddles: MySQL, PostgreSQL, SQL Server

like image 118
Martin Smith Avatar answered Sep 28 '22 08:09

Martin Smith