βͺ SQL FULL JOIN β Combine Matched and Unmatched Rows
π§² Introduction β What is SQL FULL JOIN?
A FULL JOIN (or FULL OUTER JOIN) combines all rows from both tables, returning matched rows and also unmatched rows with NULL where no match exists.
π― In this guide, youβll learn:
- Syntax and usage of
FULL JOIN - How it compares with
INNER,LEFT, andRIGHTJOINs - Workarounds for databases that donβt support it natively
β 1. FULL JOIN Syntax
SELECT columns
FROM table1
FULL JOIN table2 ON table1.key = table2.key;
β Returns rows from both tablesβeven if there is no match in the other.
π₯ 2. Example β Employees and Projects
SELECT e.name AS employee, p.name AS project
FROM employees e
FULL JOIN projects p ON e.project_id = p.id;
β Includes:
- Employees with projects
- Projects without employees
- Employees not assigned to any project
π 3. MySQL Workaround β FULL JOIN with UNION
SELECT *
FROM a
LEFT JOIN b ON a.id = b.id
UNION
SELECT *
FROM a
RIGHT JOIN b ON a.id = b.id;
β MySQL doesn’t support FULL JOIN directlyβthis emulates it.
π 4. FULL JOIN vs Other Joins
| Type | Matches from A | Matches from B | Unmatched A | Unmatched B |
|---|---|---|---|---|
| INNER JOIN | β | β | β | β |
| LEFT JOIN | β | β | β | β |
| RIGHT JOIN | β | β | β | β |
| FULL JOIN | β | β | β | β |
π 5. NULL Awareness in FULL JOIN
SELECT c.name, o.id
FROM customers c
FULL JOIN orders o ON c.id = o.customer_id
WHERE c.id IS NULL OR o.id IS NULL;
β Returns unmatched records only (i.e., anti-join use case).
π Best Practices
| β Recommended | β Avoid This |
|---|---|
| Use FULL JOIN for completeness reports | Using on large tables without filtering |
Always handle NULL values properly | Assuming all rows are matched |
Use UNION workaround in MySQL | Expecting native FULL JOIN in all RDBMS |
π Summary β Recap & Next Steps
FULL JOIN is powerful when you need a complete view of data, including unmatched rows from either side. It’s ideal for comparisons, synchronization, and gap analysis.
π Key Takeaways:
- Combines results of
LEFT JOINandRIGHT JOIN - Returns all rows with
NULLwhere no match exists - Simulate with
UNIONif unsupported
βοΈ Real-World Relevance:
Used in reporting tools, change-tracking queries, and completeness audits.
β‘οΈ Next: Explore CROSS JOIN for full combinations, or CASE for column-level conditional logic.
β FAQ β SQL FULL JOIN
β What does FULL JOIN return?
β All rows from both tables with NULLs where thereβs no match.
β Is FULL JOIN supported in MySQL?
β No. Use UNION of LEFT and RIGHT JOIN instead.
β What is the difference between FULL JOIN and INNER JOIN?
β INNER JOIN only returns matched rows; FULL JOIN returns all.
β Does FULL JOIN affect performance?
β Yes. Itβs more intensiveβfilter or index wisely.
β When should I use FULL JOIN?
β When you need all dataβeven unmatchedβfrom both sides.
Share Now :
