FULL OUTER JOIN Explained
A FULL OUTER JOIN:
- Returns all records from both tables.
- If there’s a match between A and B, it combines them.
- If there’s no match, you still get the row — with
NULLin the missing side.
Summary:
| Situation | FULL OUTER JOIN behavior |
|---|---|
| Row matches in both A and B | Combine them. |
| Row in A, but no match in B | Show A row + NULL for B. |
| Row in B, but no match in A | Show B row + NULL for A. |
Example
Customers
| customer_id | name |
|---|---|
| 1 | Alice |
| 2 | Bob |
| 3 | Charlie |
Orders
| order_id | customer_id | product |
|---|---|---|
| 101 | 1 | Laptop |
| 102 | 2 | Smartphone |
| 103 | 4 | Tablet |
Query:
SELECT Customers.customer_id, Customers.name, Orders.order_id, Orders.product
FROM Customers
FULL OUTER JOIN Orders
ON Customers.customer_id = Orders.customer_id;
Result:
| customer_id | name | order_id | product |
|---|---|---|---|
| 1 | Alice | 101 | Laptop |
| 2 | Bob | 102 | Smartphone |
| 3 | Charlie | NULL | NULL |
| NULL | NULL | 103 | Tablet |
✅ What’s happening:
- Alice and Bob match an order — perfect, rows are merged.
- Charlie has no orders — so we see his row with
NULLfororder_idandproduct. - Order
103is forcustomer_id = 4, but no such customer — socustomer_idandnameareNULLon the left side.
Why Use FULL OUTER JOIN?
- When you want to see everything: all customers, all orders — even if there’s no match.
- Useful for reporting, audits, or finding mismatches (like customers without orders, or orders without customers).
For example:
- Customers who haven’t ordered anything yet.
- Orders placed but no matching customer exists (e.g., data entry errors).
FULL OUTER JOIN = LEFT JOIN + RIGHT JOIN
You could think of it as:
SELECT * FROM Customers
LEFT JOIN Orders ON Customers.customer_id = Orders.customer_id
UNION
SELECT * FROM Customers
RIGHT JOIN Orders ON Customers.customer_id = Orders.customer_id;
But FULL OUTER JOIN does this in one step.
Limitations
- Not supported by all databases natively (e.g., MySQL doesn’t support FULL OUTER JOIN directly — you have to simulate it with
UNIONofLEFT JOINandRIGHT JOIN).
Visual (Venn Diagram)
🟢 (Customers) 🔵 (Orders)
\_________intersection_________/
FULL OUTER JOIN = 🟢 everything in both circles 🔵
Would you like me to show how to simulate a FULL OUTER JOIN in MySQL (since MySQL doesn’t have a direct FULL OUTER JOIN keyword)? 🚀✨