MySQL
Database Management
SQL Queries
Full Outer Join
Database Queries

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.

Practice system design

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:

sql
1SELECT column_name(s)
2FROM table1
3FULL OUTER JOIN table2
4ON table1.common_field = table2.common_field;

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:

  1. 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 with NULL.
  2. 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 with NULL.
  3. 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:

sql
1-- Employees table
2+-----+----------+
3| ID  | Name     |
4+-----+----------+
5| 1   | Alice    |
6| 2   | Bob      |
7| 3   | Charlie  |
8+-----+----------+
9
10-- Departments table
11+-----+--------------+
12| ID  | Department   |
13+-----+--------------+
14| 1   | HR           |
15| 2   | Marketing    |
16| 4   | Development  |
17+-----+--------------+

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:

sql
1SELECT e.ID, e.Name, d.Department
2FROM Employees e
3LEFT JOIN Departments d ON e.ID = d.ID
4UNION
5SELECT e.ID, e.Name, d.Department
6FROM Departments d
7LEFT JOIN Employees e ON d.ID = e.ID;

Key Points Table

ItemDescription
What is a FULL OUTER JOIN?A database join that combines all records from two tables regardless of matching criteria.
Simulation in MySQLCombine LEFT JOIN, RIGHT JOIN, and UNION to achieve the result of a FULL OUTER JOIN.
Example Use CaseCombine 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.