In SQL, the FULL OUTER JOIN is one of the most versatile join operations. It combines the results of both the LEFT JOIN and RIGHT JOIN to return all records from both the left and right tables. If there’s no match between the two tables, it fills the result with NULL
values for the missing side.
A FULL OUTER JOIN returns all rows when there is a match in either the left or right table. It combines the results of both LEFT JOIN and RIGHT JOIN.
NULL
for the missing side.In simpler terms:
NULL
for the right table’s columns, and vice versa.The syntax of the FULL OUTER JOIN is similar to other joins:
SELECT columns
FROM table1
FULL OUTER JOIN table2
ON table1.column = table2.column;
Let’s understand how a FULL OUTER JOIN works with a real example. We’ll use two tables: employees
and departments
.
-- employees table
+-------------+-----------------+---------------+
| employee_id | employee_name | department_id |
+-------------+-----------------+---------------+
| 1 | Alice | 1 |
| 2 | Bob | 2 |
| 3 | Charlie | NULL |
+-------------+-----------------+---------------+
-- departments table
+---------------+--------------------+
| department_id | department_name |
+---------------+--------------------+
| 1 | HR |
| 2 | Finance |
| 3 | Engineering |
+---------------+--------------------+
SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
FULL OUTER JOIN departments
ON employees.department_id = departments.department_id;
Explanation:
employee_id
, employee_name
, and department_name
.employees
table and the departments
table using the department_id
column.NULL
.Result:
employee_id | employee_name | department_name |
---|---|---|
1 | Alice | HR |
2 | Bob | Finance |
3 | Charlie | NULL |
NULL | NULL | Engineering |
Explanation of Result:
employee_name
and department_name
values are displayed.department_name
is NULL
.employee_id
and employee_name
are NULL
.You can also use a FULL OUTER JOIN to filter specific data. For example, let’s say we want to find out which departments do not have any employees and which employees are not assigned to any department.
SELECT employees.employee_id, employees.employee_name
FROM employees
FULL OUTER JOIN departments
ON employees.department_id = departments.department_id
WHERE departments.department_name IS NULL;
Explanation:
NULL
in the department_name
column.WHERE
clause filters out rows where a department is assigned to the employee.Result:
employee_id | employee_name |
---|---|
3 | Charlie |
SELECT departments.department_name
FROM employees
FULL OUTER JOIN departments
ON employees.department_id = departments.department_id
WHERE employees.employee_name IS NULL;
Explanation:
NULL
in the employee_name
column.WHERE
clause filters out rows where an employee is assigned to the department.Result:
department_name |
---|
Engineering |
You might wonder how a FULL OUTER JOIN differs from LEFT JOIN and RIGHT JOIN.
NULL
is returned for columns from the right table.NULL
is returned for columns from the left table.NULL
is returned for the missing side.Let’s see how LEFT JOIN and RIGHT JOIN compare:
SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;
Result for LEFT JOIN:
employee_id | employee_name | department_name |
---|---|---|
1 | Alice | HR |
2 | Bob | Finance |
3 | Charlie | NULL |
SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.department_id;
Result for RIGHT JOIN:
employee_id | employee_name | department_name |
---|---|---|
1 | Alice | HR |
2 | Bob | Finance |
NULL | NULL | Engineering |
Key Differences:
NULL
where there are no matches.