π The FULL OUTER JOIN (often shortened to FULL JOIN) returns all rows from both tables. When a matching row exists in both tables, the data is combined into a single result. If no match exists, SQL returns NULL for the missing columns from the opposite table.
π What is a FULL OUTER JOIN?
A FULL OUTER JOIN combines the behavior of LEFT JOIN and RIGHT JOIN. It includes:
- β All matching rows from both tables.
- β All unmatched rows from the left table.
- β All unmatched rows from the right table.
Information
π― Why Use FULL OUTER JOIN?
Use a FULL OUTER JOIN when you need a complete view of data from both tables, including records that do not have matching values.
- π Compare two datasets.
- π Identify missing relationships.
- π Merge information from multiple sources.
- π Perform data auditing and validation.
π Sample Tables
Consider the following Students table:
| StudentID | Name | Department |
|---|---|---|
| 101 | Alice | Computer Science |
| 102 | Bob | Mathematics |
| 103 | Charlie | Physics |
| 104 | David | Computer Science |
Consider the following Courses table:
| CourseID | StudentID | CourseName |
|---|---|---|
| 1 | 101 | Database Systems |
| 2 | 101 | Operating Systems |
| 3 | 103 | Physics Lab |
| 4 | 104 | Computer Networks |
| 5 | 105 | Artificial Intelligence |
π Basic Syntax
FULL OUTER JOIN Syntax
SELECT table1.column_name,
table2.column_name
FROM table1
FULL OUTER JOIN table2
ON table1.common_column = table2.common_column;π‘ Basic FULL OUTER JOIN Example
Retrieve every student and every course, whether they match or not.
Basic FULL OUTER JOIN
SELECT Students.Name,
Courses.CourseName
FROM Students
FULL OUTER JOIN Courses
ON Students.StudentID = Courses.StudentID;Result:
| Name | CourseName |
|---|---|
| Alice | Database Systems |
| Alice | Operating Systems |
| Bob | NULL |
| Charlie | Physics Lab |
| David | Computer Networks |
| NULL | Artificial Intelligence |
Important
π·οΈ Using Table Aliases
Aliases improve readability and reduce typing in complex queries.
FULL OUTER JOIN with Aliases
SELECT s.Name,
c.CourseName
FROM Students AS s
FULL OUTER JOIN Courses AS c
ON s.StudentID = c.StudentID;π Finding Unmatched Rows
You can use FULL OUTER JOIN together with IS NULL to identify records that exist in only one table.
Find Unmatched Records
SELECT s.Name,
c.CourseName
FROM Students s
FULL OUTER JOIN Courses c
ON s.StudentID = c.StudentID
WHERE s.StudentID IS NULL
OR c.StudentID IS NULL;This query returns:
- Students without any course.
- Courses without any student.
π FULL OUTER JOIN with ORDER BY
Sort the combined results using the ORDER BY clause.
Sort FULL OUTER JOIN Results
SELECT s.Name,
c.CourseName
FROM Students s
FULL OUTER JOIN Courses c
ON s.StudentID = c.StudentID
ORDER BY s.Name;π FULL OUTER JOIN Multiple Tables
You can combine multiple tables by performing additional joins.
FULL OUTER JOIN Three Tables
SELECT s.Name,
c.CourseName,
d.DepartmentName
FROM Students s
FULL OUTER JOIN Courses c
ON s.StudentID = c.StudentID
FULL OUTER JOIN Departments d
ON s.DepartmentID = d.DepartmentID;π How FULL OUTER JOIN Works
| Students Table | Courses Table | Included? |
|---|---|---|
| Matching StudentID | Matching StudentID | β Yes |
| No Matching Course | No Match | β Yes (Course columns are NULL) |
| No Matching Student | Course Exists | β Yes (Student columns are NULL) |
βοΈ Comparison of SQL Joins
| Join Type | Rows Returned |
|---|---|
| INNER JOIN | Only matching rows. |
| LEFT JOIN | All left rows and matching right rows. |
| RIGHT JOIN | All right rows and matching left rows. |
| FULL OUTER JOIN | All rows from both tables. |
β οΈ Database Compatibility
Important
π οΈ Simulating FULL OUTER JOIN in MySQL
FULL OUTER JOIN Alternative for MySQL
SELECT s.Name,
c.CourseName
FROM Students s
LEFT JOIN Courses c
ON s.StudentID = c.StudentID
UNION
SELECT s.Name,
c.CourseName
FROM Students s
RIGHT JOIN Courses c
ON s.StudentID = c.StudentID;β οΈ Common Mistakes
- β Assuming every database supports FULL OUTER JOIN.
- β Forgetting to handle NULL values in unmatched rows.
- β Joining tables using incorrect columns.
- β Using = NULL instead of IS NULL.
Warning
πΌ Real-World Example
A university administrator wants a report showing every student and every course, including students without enrollments and courses without assigned students.
Complete Enrollment Report
SELECT s.StudentID,
s.Name,
c.CourseName
FROM Students s
FULL OUTER JOIN Courses c
ON s.StudentID = c.StudentID
ORDER BY s.StudentID;This query provides a complete picture of student-course relationships, including missing or unmatched records.
β οΈ Best Practices
Best Practice
π Key Points to Remember
- π FULL OUTER JOIN returns all rows from both tables.
- π Matching rows are merged into a single result.
- π Unmatched rows contain NULL for missing columns.
- π It combines the behavior of LEFT JOIN and RIGHT JOIN.
- π It is useful for comparing datasets and finding missing relationships.
- π Some databases require a UNION-based alternative.