How can I do a FULL OUTER JOIN in MySQL?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
MySQL does not support FULL OUTER JOIN as a native SQL clause, so you have to build the result from other join operations. The standard workaround is reliable, but it only stays correct if you think carefully about duplicates, composite keys, and how unmatched rows should appear.
What a Full Outer Join Should Return
A true full outer join includes three categories of rows:
- rows with matching keys in both tables
- rows that exist only in the left table
- rows that exist only in the right table
In PostgreSQL or SQL Server, one clause can express that directly. In MySQL, you have to assemble the same effect manually.
Use LEFT JOIN Plus Right-Only Rows
The most common pattern is:
- select all rows from the left table with matches from the right
- add rows from the right table that had no match on the left
- combine both result sets with
UNION ALL
The first branch returns matched rows plus left-only rows. The second branch adds right-only rows, filtered by WHERE a.id IS NULL.
Prefer UNION ALL Unless You Truly Need Deduplication
Many people reach for plain UNION, but that changes semantics because it removes duplicates. In join workloads, duplicates may be legitimate and may reflect real cardinality in the data.
UNION ALL is usually the right choice because:
- it keeps the original row multiplicity
- it avoids unnecessary deduplication cost
- it matches the standard full-join emulation pattern
If you later need deduplicated business output, apply that explicitly after the join instead of mixing it into the join itself.
Keep Composite Keys Consistent
When the join key uses more than one column, the right-only filter must match the same logic as the join condition. Otherwise you will accidentally misclassify rows.
That filter must be aligned with the actual join rule. If the key logic is more complex, the unmatched-row filter must be equally careful.
Use a Key-Set Strategy for Readability
For some reporting queries, it is easier to build the union of keys first and then join both tables to that key set. In MySQL 8, a common table expression can make that readable.
This can be easier to extend when more than two tables share the same business key.
Think About Performance Early
Full-join emulation can be expensive on large tables. The most important practical steps are:
- index the join keys on both sides
- avoid
SELECT * - pre-filter rows before joining when possible
- check the plan with
EXPLAIN
Performance problems are often caused by missing indexes, not by the emulation pattern itself.
Common Pitfalls
The most common mistake is assuming MySQL supports native FULL OUTER JOIN syntax and then debugging a parser error that will never go away. Another is using UNION by default and accidentally removing duplicates that should remain visible. Developers also forget that the unmatched-row filter must align with the exact join key, especially for composite keys.
Summary
- MySQL does not support native
FULL OUTER JOIN, so you must emulate it. - The standard approach is
LEFT JOINplus a right-only branch combined withUNION ALL. - Keep unmatched-row filters aligned with the real join key logic.
- Consider a key-set CTE pattern when readability matters more than minimal SQL length.
- Index join columns and inspect execution plans before blaming the workaround itself.
Related reading
- How can I do an UPDATE statement with JOIN in SQL Server?
- How can I do 'insert if not exists' in MySQL?
- How can I do 'insert if not exists' in MySQL?
- How can I do three table JOINs in an UPDATE query?
- How can I drop all the tables in a PostgreSQL database?
- How can I easily delete all objects in a Realm
- How can I edit a view using phpMyAdmin 3.2.4?
- How can I find all the tables in MySQL with specific column names in them?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.