OUTER JOIN
SQL OUTER JOIN: Comprehensive Explanation
An OUTER JOIN retrieves matching rows from two tables and also includes non-matching rows from one or both tables, depending on the type of OUTER JOIN used. It is useful for finding data that exists in one table but not the other.
There are three types of OUTER JOINs:
LEFT OUTER JOIN (or simply
LEFT JOIN): Includes all rows from the left table and matching rows from the right table. If no match is found in the right table,NULLis returned for those columns.RIGHT OUTER JOIN (or simply
RIGHT JOIN): Includes all rows from the right table and matching rows from the left table. If no match is found in the left table,NULLis returned for those columns.FULL OUTER JOIN: Combines the results of
LEFT JOINandRIGHT JOIN. Includes all rows from both tables, withNULLfor non-matching rows.
Syntax
LEFT JOIN:
RIGHT JOIN:
FULL OUTER JOIN:
Example Scenario
We will use two tables:
1. Customers Table:
CustomerID | Name | Country |
|---|---|---|
1 | Alice | USA |
2 | Bob | Canada |
3 | Charlie | UK |
4 | David | Germany |
2. Orders Table:
OrderID | CustomerID | Product |
|---|---|---|
101 | 1 | Laptop |
102 | 2 | Tablet |
103 | 1 | Smartphone |
104 | 5 | Monitor |
1. LEFT OUTER JOIN
Query to retrieve all customers and their orders, even if some customers have not placed any orders:
Result:
CustomerID | Name | Product |
|---|---|---|
1 | Alice | Laptop |
1 | Alice | Smartphone |
2 | Bob | Tablet |
3 | Charlie | NULL |
4 | David | NULL |
Explanation:
Rows for Charlie (
CustomerID = 3) and David (CustomerID = 4) are included even though they have no matching orders.NULLis used for theProductcolumn where there is no match.
2. RIGHT OUTER JOIN
Query to retrieve all orders and their associated customers, even if some orders were placed by non-existent customers:
Result:
OrderID | Product | Name |
|---|---|---|
101 | Laptop | Alice |
103 | Smartphone | Alice |
102 | Tablet | Bob |
NULL | NULL | Charlie |
NULL | NULL | David |
Explanation:
Orders placed by non-existent customers (like
OrderID = 104withCustomerID = 5) are excluded because the right table (Customers) takes precedence.Customers without orders (like Charlie and David) are included, with
NULLforOrderIDandProduct.
3. FULL OUTER JOIN
Query to retrieve all customers and orders, including unmatched rows from both tables:
Result:
CustomerID | Name | OrderID | Product |
|---|---|---|---|
1 | Alice | 101 | Laptop |
1 | Alice | 103 | Smartphone |
2 | Bob | 102 | Tablet |
3 | Charlie | NULL | NULL |
4 | David | NULL | NULL |
NULL | NULL | 104 | Monitor |
Explanation:
Combines the results of
LEFT JOINandRIGHT JOIN.Includes all customers and all orders, even if there is no match.
NULLis used for missing values.
Comparison of Outer Joins
Join Type | Included Rows |
|---|---|
LEFT JOIN | All rows from the left table, plus matching rows from the right table. Non-matching right rows are |
RIGHT JOIN | All rows from the right table, plus matching rows from the left table. Non-matching left rows are |
FULL JOIN | All rows from both tables, with |
Key Points
Performance Considerations:
Outer joins can be slower than inner joins because they process additional rows (non-matching ones).
Use Cases:
LEFT JOIN: Retrieve all data from one table, regardless of matches in the other.RIGHT JOIN: Useful when you want all data from the second table regardless of matches.FULL JOIN: When you need a complete view of matching and non-matching rows from both tables.