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.
A FULL OUTER JOIN is a type of database join that returns a result set that includes all rows from both participating tables where the join condition is met, as well as any rows from one table that do not have a corresponding match in the other table. In MySQL, this functionality is not directly provided with a specific FULL OUTER JOIN operator, unlike some other SQL databases. However, you can simulate a FULL OUTER JOIN using a combination of LEFT JOIN, RIGHT JOIN, and UNION.
Understanding FULL OUTER JOIN Through SQL Standard
To comprehend what a simulated FULL OUTER JOIN in MySQL involves, it's essential to understand how it operates in databases that support it directly. Here is a quick SQL standard example:
This SQL command will return all records from both table1 and table2, with matching records from both sides where available. If there is no match, the result will still show the record from one table with NULL in the place of the columns of the other table.
Simulating FULL OUTER JOIN in MySQL
Since MySQL doesn't support FULL OUTER JOIN natively, you must use a combination of LEFT JOIN, RIGHT JOIN, and UNION to achieve the same results. Here's how you can do it:
- LEFT JOIN to select all records from the first table (
table1) and the matched records from the second table (table2), plus the non-matched records withNULL. - RIGHT JOIN to select all records from the second table (
table2) and the matched records from the first table (table1), plus the non-matched records withNULL. - UNION to merge the results of the LEFT JOIN and RIGHT JOIN.
Example SQL Query to Simulate FULL OUTER JOIN
If you have two tables, Employees and Departments:
Here is how you can simulate a FULL OUTER JOIN to find all employees and their respective departments, including those without a department and those departments without an employee:
Key Points Table
| Item | Description |
| What is a FULL OUTER JOIN? | A database join that combines all records from two tables regardless of matching criteria. |
| Simulation in MySQL | Combine LEFT JOIN, RIGHT JOIN, and UNION to achieve the result of a FULL OUTER JOIN. |
| Example Use Case | Combine employee and department tables to list all employees and their departments. |
Conclusion
While MySQL does not support the FULL OUTER JOIN syntax directly, using a combination of LEFT JOIN, RIGHT JOIN, and UNION allows you to efficiently simulate this type of join. This method ensures that you do not miss any data from either table, thus preserving the integrity and completeness of your data analysis or query results.
Related reading
- How can I do a FULL OUTER JOIN in MySQL?
- 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?

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.