SQL Outer Join
This lesson 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
| Code |
|
Source Tables
Left Table
| Id | FirstName | LastName | UserName |
| 1 | Fred | Flinstone | freddo |
| 2 | Homer | Simpson | homey |
| 3 | Homer | Brown | notsofamous |
| 4 | Ozzy | Ozzbourne | sabbath |
| 5 | Homer | Gain | noplacelike |
Right Table
| IndividualId | AccessLevel |
| 1 | Administrator |
| 2 | Contributor |
| 3 | Contributor |
| 4 | Contributor |
| 10 | Administrator |
Result
| IndividualId | FirstName | LastName | UserName | IndividualId | AccessLevel |
| 1 | Fred | Flinstone | freddo | 1 | Administrator |
| 2 | Homer | Simpson | homey | 2 | Contributor |
| 3 | Homer | Brown | notsofamous | 3 | Contributor |
| 4 | Ozzy | Osbourne | sabbath | 4 | Contributor |
| 5 | Homer | Gain | noplacelike | NULL | NULL |
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
| Code |
|
Source Tables
Left Table
| Id | FirstName | LastName | UserName |
| 1 | Fred | Flinstone | freddo |
| 2 | Homer | Simpson | homey |
| 3 | Homer | Brown | notsofamous |
| 4 | Ozzy | Ozzbourne | sabbath |
| 5 | Homer | Gain | noplacelike |
Right Table
| IndividualId | AccessLevel |
| 1 | Administrator |
| 2 | Contributor |
| 3 | Contributor |
| 4 | Contributor |
| 10 | Administrator |
Result
| IndividualId | FirstName | LastName | UserName | IndividualId | AccessLevel |
| 1 | Fred | Flinstone | freddo | 1 | Administrator |
| 2 | Homer | Simpson | homey | 2 | Contributor |
| 3 | Homer | Brown | notsofamous | 3 | Contributor |
| 4 | Ozzy | Osbourne | sabbath | 4 | Contributor |
| NULL | NULL | NULL | NULL | 10 | Administrator |
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
| Code |
|
Source Tables
Left Table
| Id | FirstName | LastName | UserName |
| 1 | Fred | Flinstone | freddo |
| 2 | Homer | Simpson | homey |
| 3 | Homer | Brown | notsofamous |
| 4 | Ozzy | Ozzbourne | sabbath |
| 5 | Homer | Gain | noplacelike |
Right Table
| IndividualId | AccessLevel |
| 1 | Administrator |
| 2 | Contributor |
| 3 | Contributor |
| 4 | Contributor |
| 10 | Administrator |
Result
| IndividualId | FirstName | LastName | UserName | IndividualId | AccessLevel |
| 1 | Fred | Flinstone | freddo | 1 | Administrator |
| 2 | Homer | Simpson | homey | 2 | Contributor |
| 3 | Homer | Brown | notsofamous | 3 | Contributor |
| 4 | Ozzy | Osbourne | sabbath | 4 | Contributor |
| 5 | Homer | Gain | noplacelike | NULL | NULL |
| NULL | NULL | NULL | NULL | 10 | Administrator |
Enjoy this page?
-
- Link to this page (copy/paste into your own website or blog):
- Link to Quackit using one of these banner ads.
Thanks for supporting Quackit!