your image

SQL Outer Join

quackit
Related Topic
:- SQL

This page covers both the left outer join, the right outer join, and the full outer join, and explains the differences between them.

There are some occasions where you would need to use a left outer join or a right outer join, and others where you would need a full outer join. The join type you use will depend on the situation and what data you need to return.

Left Outer Join

Use this when you only want to return rows that have matching data in the left table, even if there's no matching rows in the right table.

Example SQL statement

 

SELECT * FROM Individual AS Ind
LEFT JOIN Publisher AS Pub
ON Ind.IndividualId = Pub.IndividualId;

Source Tables

Left Table

IndividualIdFirstNameLastNameUserName1FredFlinstonefreddo2HomerSimpsonhomey3HomerBrownnotsofamous4OzzyOzzbournesabbath5HomerGainnoplacelike

Right Table

IndividualIdAccessLevel1Administrator2Contributor3Contributor4Contributor10Administrator

Result

IndividualIdFirstNameLastNameUserNameIndividualIdAccessLevel1FredFlinstonefreddo1Administrator2HomerSimpsonhomey2Contributor3HomerBrownnotsofamous3Contributor4OzzyOsbournesabbath4Contributor5HomerGainnoplacelikeNULLNULL

Note the following:

  • The right table has a row with an IndividualId of 10. The left join doesn't return that row, because there's no corresponding value in the left table.
  • The left table has a row with an IndividualId of 5. The left join does return that row, even though there is no corresponding value in the right table.

Right Outer Join

Use this when you only want to return rows that have matching data in the right table, even if there's no matching rows in the left table.

Example SQL statement

 

 
SELECT * FROM Individual AS Ind
RIGHT JOIN Publisher AS Pub
ON Ind.IndividualId = Pub.IndividualId;

Source Tables

Left Table

IndividualIdFirstNameLastNameUserName1FredFlinstonefreddo2HomerSimpsonhomey3HomerBrownnotsofamous4OzzyOzzbournesabbath5HomerGainnoplacelike

Right Table

IndividualIdAccessLevel1Administrator2Contributor3Contributor4Contributor10Administrator

Result

IndividualIdFirstNameLastNameUserNameIndividualIdAccessLevel1FredFlinstonefreddo1Administrator2HomerSimpsonhomey2Contributor3HomerBrownnotsofamous3Contributor4OzzyOsbournesabbath4ContributorNULLNULLNULLNULL10Administrator

This time we get a row for the IndividualId of 10, but not for the IndividualId of 5.

Full Outer Join

Use this when you want to all rows, even if there's no matching rows in the right table.

Example SQL statement

 

 
SELECT * FROM Individual AS Ind
FULL JOIN Publisher AS Pub
ON Ind.IndividualId = Pub.IndividualId;

Source Tables

Left Table

IndividualIdFirstNameLastNameUserName1FredFlinstonefreddo2HomerSimpsonhomey3HomerBrownnotsofamous4OzzyOzzbournesabbath5HomerGainnoplacelike

Right Table

IndividualIdAccessLevel1Administrator2Contributor3Contributor4Contributor10Administrator

Result

IndividualIdFirstNameLastNameUserNameIndividualIdAccessLevel1FredFlinstonefreddo1Administrator2HomerSimpsonhomey2Contributor3HomerBrownnotsofamous3Contributor4OzzyOsbournesabbath4Contributor5HomerGainnoplacelikeNULLNULLNULLNULLNULLNULL10Administrator

This time we get a row for the IndividualId of 10 and another row for the IndividualId of 5.

Comments