FULL OUTER JOIN

Updated · 9 min read
mediumPostgreSQL 18sqlfull-joinouter-joinjoinsmysql

FULL OUTER JOIN keeps every row from both tables: matched rows are paired, and unmatched rows from either side appear with NULLs on the other. It's a left join and a right join at once. Its classic use is comparing two lists: what's in both, what's only in the first, and what's only in the second. In this lesson, students have asked for courses, and the program wants to see the whole picture: requested courses it offers, courses nobody requested, and requests for courses it doesn't offer at all.

In One Minute

  • FROM a FULL JOIN b ON ... keeps every row of a and every row of b.
  • Matched rows are paired; unmatched rows get NULL in the other table's columns.
  • WHERE a.key IS NULL OR b.key IS NULL keeps only the rows without a partner.
  • coalesce(a.col, b.col) gives one column that's never NULL, handy for sorting.
  • MySQL has no FULL JOIN; combine a LEFT JOIN and an anti-join with UNION ALL.
FULL JOIN keeps both sides; filtering for NULLs keeps only the unmatched rows

The Setup: Course Requests

Students can ask for courses. Navika has asked for Python Basics and Git Basics, and Advitha for Git Basics. The requests are stored by title:

SQL
CREATE TABLE course_requests (
  student_id INTEGER NOT NULL REFERENCES students (student_id),
  title      VARCHAR(60) NOT NULL
);
 
INSERT INTO course_requests VALUES
  (103, 'Python Basics'),
  (102, 'Git Basics'),
  (103, 'Git Basics');

Each example below runs this setup first. Python Basics isn't a course the program offers, so it exists on one side only; most offered courses have no requests, so they exist on the other side only.

The Basics

SQL
SELECT columns
FROM table_a
FULL OUTER JOIN table_b ON table_a.key = table_b.key;

Every offered course and every request, matched by title:

SQL▶ Try it
CREATE TABLE course_requests (student_id INTEGER NOT NULL REFERENCES students (student_id), title VARCHAR(60) NOT NULL);
INSERT INTO course_requests VALUES (103, 'Python Basics'), (102, 'Git Basics'), (103, 'Git Basics');
 
SELECT c.title AS offered, r.title AS requested, r.student_id
FROM courses AS c
FULL JOIN course_requests AS r ON r.title = c.title
ORDER BY coalesce(c.title, r.title), r.student_id;
Output
CREATE TABLE
 
INSERT 0 3
 
      offered      |   requested   | student_id
-------------------+---------------+------------
 Git Basics        | Git Basics    |        102
 Git Basics        | Git Basics    |        103
 Java Basics       | NULL          |       NULL
 Java Collections  | NULL          |       NULL
 Java OOP          | NULL          |       NULL
 JavaScript Basics | NULL          |       NULL
 NULL              | Python Basics |        103
 SQL Basics        | NULL          |       NULL
(8 rows)

Three kinds of row, all in one result:

KindExampleColumns
MatchedGit Basics, requested twiceBoth sides filled
Only in coursesJava Basics, nobody requested itrequested and student_id are NULL
Only in course_requestsPython Basics, not offeredoffered is NULL

FULL JOIN and FULL OUTER JOIN mean the same. Sorting by coalesce(c.title, r.title) puts each row in title order whichever side it came from.

Matched pairs in the middle; courses nobody requested on the left; Python Basics, requested but not offered, on the right

Comparing Two Lists

Keep only the rows without a partner to see the differences between two lists:

SQL▶ Try it
CREATE TABLE course_requests (student_id INTEGER NOT NULL REFERENCES students (student_id), title VARCHAR(60) NOT NULL);
INSERT INTO course_requests VALUES (103, 'Python Basics'), (102, 'Git Basics'), (103, 'Git Basics');
 
SELECT c.title AS offered, r.title AS requested
FROM courses AS c
FULL JOIN course_requests AS r ON r.title = c.title
WHERE c.course_id IS NULL OR r.title IS NULL
ORDER BY coalesce(c.title, r.title);
Output
CREATE TABLE
 
INSERT 0 3
 
      offered      |   requested
-------------------+---------------
 Java Basics       | NULL
 Java Collections  | NULL
 Java OOP          | NULL
 JavaScript Basics | NULL
 NULL              | Python Basics
 SQL Basics        | NULL
(6 rows)

Five offered courses nobody asked for, and one requested course the program doesn't offer: a to-do list for whoever plans next term. The same pattern compares any two lists: last month's students with this month's, or a spreadsheet import with the table it should match.

FULL JOIN in MySQL

MySQL doesn't support FULL OUTER JOIN. You build it from a left join, which gives the matched rows plus the left-only rows, and add the right-only rows with an anti-join, glued together by UNION ALL:

SQL▶ Try it
CREATE TABLE course_requests (student_id INTEGER NOT NULL REFERENCES students (student_id), title VARCHAR(60) NOT NULL);
INSERT INTO course_requests VALUES (103, 'Python Basics'), (102, 'Git Basics'), (103, 'Git Basics');
 
SELECT c.title AS offered, r.title AS requested, r.student_id
FROM courses AS c
LEFT JOIN course_requests AS r ON r.title = c.title
 
UNION ALL
 
SELECT c.title, r.title, r.student_id
FROM course_requests AS r
LEFT JOIN courses AS c ON c.title = r.title
WHERE c.course_id IS NULL
 
ORDER BY offered NULLS LAST, requested, student_id;
Output
CREATE TABLE
 
INSERT 0 3
 
      offered      |   requested   | student_id
-------------------+---------------+------------
 Git Basics        | Git Basics    |        102
 Git Basics        | Git Basics    |        103
 Java Basics       | NULL          |       NULL
 Java Collections  | NULL          |       NULL
 Java OOP          | NULL          |       NULL
 JavaScript Basics | NULL          |       NULL
 SQL Basics        | NULL          |       NULL
 NULL              | Python Basics |        103
(8 rows)

The same eight rows. The second half adds only requests with no course, so nothing is counted twice; that's why it's UNION ALL with an anti-join, not a plain UNION of two outer joins, which would also remove real duplicate rows. UNION itself is the subject of UNION and UNION ALL. This query runs in PostgreSQL too, so you can test it here; in MySQL, write the ORDER BY without NULLS LAST, since MySQL already sorts NULL first and lacks that keyword.

LEFT JOIN gives matches and courses-only rows; the anti-join adds requests-only rows; UNION ALL stacks them

Common Mistakes

Filtering one side in WHERE. A condition like WHERE r.student_id = 103 drops every row where r is NULL, undoing the full join. Put it in ON, or filter before joining.

Sorting by one side's column. Rows from the other side have NULL there. Sort by coalesce(a.col, b.col).

Using FULL JOIN when every row has a partner. Every enrollment has a student, so students FULL JOIN enrollments gives the same rows as a left join. Use full joins where both sides can have extras.

Interview Corner

What does FULL OUTER JOIN return?

Every row from both tables. Matching rows are combined; rows with no match on the other side appear with NULL in that side's columns.

How do you do a FULL OUTER JOIN in MySQL?

MySQL doesn't support it. Take a LEFT JOIN, then UNION ALL the rows from the right table with no match on the left (a LEFT JOIN the other way with WHERE left.key IS NULL).

When would you use a FULL OUTER JOIN?

To compare two lists or reconcile two data sources: it shows what's in both, what's only in the first, and what's only in the second, in one result.

Practice

Challenge: With the same course_requests setup, list each title that appears on either side, with a label: 'both' if it's offered and requested, 'offered only', or 'requested only'. Show each title once (use DISTINCT) and sort by title. Hint: use CASE, covered fully in CASE Expressions: CASE WHEN c.course_id IS NULL THEN 'requested only' WHEN r.title IS NULL THEN 'offered only' ELSE 'both' END.

Show solution
SQL▶ Try it
CREATE TABLE course_requests (student_id INTEGER NOT NULL REFERENCES students (student_id), title VARCHAR(60) NOT NULL);
INSERT INTO course_requests VALUES (103, 'Python Basics'), (102, 'Git Basics'), (103, 'Git Basics');
 
SELECT DISTINCT
       coalesce(c.title, r.title) AS title,
       CASE WHEN c.course_id IS NULL THEN 'requested only'
            WHEN r.title IS NULL THEN 'offered only'
            ELSE 'both'
       END AS status
FROM courses AS c
FULL JOIN course_requests AS r ON r.title = c.title
ORDER BY title;
Output
CREATE TABLE
 
INSERT 0 3
 
       title       |     status
-------------------+----------------
 Git Basics        | both
 Java Basics       | offered only
 Java Collections  | offered only
 Java OOP          | offered only
 JavaScript Basics | offered only
 Python Basics     | requested only
 SQL Basics        | offered only
(7 rows)

Git Basics is the only title on both lists; DISTINCT folds its two request rows into one.

Quick check: FULL OUTER JOIN

3 questions
1Which rows does courses FULL JOIN course_requests keep?
2Python Basics is requested but not offered. What is its offered column?
3How do you get a full join in MySQL?

Key Takeaways

You want toWrite
Both sides, matchedFROM courses c FULL JOIN course_requests r ON r.title = c.title
Only the differences... WHERE c.course_id IS NULL OR r.title IS NULL
One title columncoalesce(c.title, r.title)

Was this tutorial helpful?

Lesson 39 of 115