MySQL does not support FULL OUTER JOIN, so you simulate it by combining a LEFT JOIN and a RIGHT JOIN with UNION. The result contains every row from both tables: matched pairs once, unmatched left rows with NULL on the right, and unmatched right rows with NULL on the left. The mysql full outer join workaround is short once you understand why UNION is the right operator.
This guide uses customers(id, name, city) and orders(id, customer_id, total) from the related join articles.
A full outer join is the union of a LEFT JOIN and a RIGHT JOIN. With this data:
you want Asha and Ben with their orders, Chen with no order, and order 13 with no customer. Writing FULL OUTER JOIN in MySQL gives a syntax error.
Both queries must return the same number of columns with compatible types. Column names come from the first query.
Asha's and Ben's rows appear in both halves, because both a LEFT JOIN and a RIGHT JOIN include matched rows. UNION removes duplicate rows, so each matched pair appears once. UNION ALL would keep both copies and double the matched rows.
The catch: UNION removes exact duplicates across the whole row. If your data legitimately contains two identical rows, for example two identical order lines for the same customer with no distinguishing column, UNION will collapse them. Include a unique column such as o.id in the select list to avoid that, as above.
For large tables, deduplicating costs a sort or temporary table. A more precise and often faster pattern uses UNION ALL and removes the overlap by hand: the LEFT JOIN part, plus only the right rows that had no match.
The WHERE c.id IS NULL keeps only orders with no matching customer, so nothing is duplicated and genuinely repeated rows are preserved. It returns the same four rows as before.
To see which side each row came from, add a flag column or use COALESCE:
This is handy for reconciliation reports that compare two data sets and list what exists on one side only.
An ORDER BY after the last query applies to the whole union:
Wrap each SELECT in parentheses when it has its own ORDER BY or LIMIT. To filter the combined result, wrap the union in a derived table: SELECT * FROM (...) AS t WHERE ....
No. Current MySQL 8.x and 9.x releases have no FULL OUTER JOIN keyword; use the UNION pattern. MariaDB and PostgreSQL differ, so check your database.
Use UNION for the simple version, which removes the duplicated matched rows. Use UNION ALL with an anti-join WHERE for the exact and faster version.
Every SELECT in a union must return the same number of columns. List the columns explicitly instead of using * with different tables.
Yes, but it gets complicated quickly. Join two tables first in a subquery or common table expression, then combine the result with the third.
MySQL lacks FULL OUTER JOIN, but a LEFT JOIN combined with a RIGHT JOIN through UNION gives the same rows. Use UNION ALL plus WHERE left.pk IS NULL when you need exact duplicates and better performance, keep column lists identical in both queries, and add a source column when you need to know which side a row came from.