The FULL OUTER JOIN is the most inclusive type of join. It returns all rows when there is a match in either the left or the right table. It is essentially a combination of a LEFT JOIN and a RIGHT JOIN.
A FULL OUTER JOIN creates a result set that includes all records from both tables.
Visualization:
Table A (potential_leads) Table B (active_clients)
+----+----------+ +----+----------+
| id | name | | id | status |
+----+----------+ +----+----------+
| 1 | Alice | <--------> | 1 | Active | (Match!)
| 2 | Bob | <--------? | NULL | (Lead only, no account yet)
| NULL | <--------> | 3 | Active | (Client only, not in lead list)
+----+----------+ +----+----------+
SELECT
table1.column1,
table2.column2
FROM
table1
FULL OUTER JOIN
table2 ON table1.common_column = table2.common_column;
Important Note on Database Support: Not all database systems support
FULL OUTER JOINnatively.
- PostgreSQL, SQL Server, and Oracle support it.
- MySQL and MariaDB do NOT support it.
Since MySQL doesn't have FULL OUTER JOIN, developers achieve the same result by combining a LEFT JOIN and a RIGHT JOIN using the UNION operator:
SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id
UNION
SELECT * FROM table1 RIGHT JOIN table2 ON table1.id = table2.id;
Imagine we are merging data from two different branch offices. Branch A has their own list of customers, and Branch B has theirs. We want a complete list of all customers across both branches, showing where they overlap.
SELECT
a.customer_name AS branch_a_name,
b.customer_name AS branch_b_name
FROM
branch_a_customers AS a
FULL OUTER JOIN
branch_b_customers AS b ON a.customer_id = b.customer_id;