LEFT, RIGHT and FULL joins
keep the rows that didn't match, padded with NULLs. and the WHERE that turns them back into inner joins.
- address
- everything.joins.outer_joins
- part
- part 3, joins and subqueries
- chapter
- chapter 3 of 11
- reading time
- 9 min
a LEFT JOIN returns every pair that matched, plus each left row that matched nothing, once, with NULLs in the right table's columns. RIGHT JOIN does the same for the right table, and FULL JOIN for both.
a WHERE on the right table runs after the padding and throws it away. put that condition in ON instead.
the picture
you know how you ask for every instructor with their courses, and the list comes back one instructor short? Sofia Petrova teaches nothing this year, so an inner join has nothing to pair her with, and she's just not there.
think of a class photo. everyone who showed up sits in their seat. a LEFT JOIN also keeps a seat for every kid who stayed home: an empty chair with their name card on it. the name card is the left row, the empty chair is the NULLs.
| courses.instructor_id ↓ instructors.id → | 3 Lena Fischer | 6 Amara Diallo | 7 Oliver Grant | 8 Sofia Petrova |
|---|---|---|---|---|
| 3 CS401 | ✓ | · | · | · |
| NULL CS499 | · | · | · | · |
| 7 HIST101 | · | · | ✓ | · |
| 6 PHYS101 | · | ✓ | · | · |
| 6 PHYS201 | · | ✓ | · | · |
| code | instructor_id | id | name |
|---|---|---|---|
| CS401 | 3 | 3 | Lena Fischer |
| HIST101 | 7 | 7 | Oliver Grant |
| PHYS101 | 6 | 6 | Amara Diallo |
| PHYS201 | 6 | 6 | Amara Diallo |
| CS499 | null | null | null |
the 4 matching pairs, plus 1 leftover row padded with NULLs (marked ref above): an outer join never loses a row from its outer side.
the figure uses five of the 12 courses and four of the 8 instructors, so the grid fits. the full tables tell the same story, which is what the rest of this chapter runs.
LEFT JOIN
every instructor, with their courses if they have any:
SELECT i.name, c.code, c.title
FROM instructors AS i
LEFT JOIN courses AS c ON c.instructor_id = i.id
ORDER BY i.id, c.code;| nametext | codetext | titletext |
|---|---|---|
| Hannah Okoye | CS101 | Intro to Programming |
| Hannah Okoye | CS301 | Databases |
| Marco Rossi | CS201 | Data Structures |
| Marco Rossi | CS350 | Operating Systems |
| Lena Fischer | CS401 | Database Internals |
| Tomás Ortega | MATH101 | Calculus I |
| Tomás Ortega | MATH301 | Linear Algebra |
| Yuki Tanaka | MATH201 | Discrete Math |
| Amara Diallo | PHYS101 | Mechanics |
| Amara Diallo | PHYS201 | Electromagnetism |
| Oliver Grant | HIST101 | History of Computing |
| Sofia Petrova | null | null |
12 rows: 11 matched pairs (Hannah Okoye teaches two courses, so she's in two rows), plus Sofia Petrova, once, with NULL for code and title. nobody stored those NULLs. they're the join's way of writing "no partner". (LEFT OUTER JOIN is the same thing. the OUTER is optional and almost nobody types it.)
"left" just means the table written before the keyword. that's the one that keeps all its rows. a LEFT JOIN can't return fewer rows than its left table, and it can return more: one row per match, and at least one per left row.
RIGHT and FULL
RIGHT JOIN keeps every row of the table written after the keyword. a RIGHT JOIN b is b LEFT JOIN a with the columns in a different order, so most people only ever write LEFT and put the table they care about first.
FULL JOIN keeps the leftovers from both sides. every course with its instructor, and every instructor with their course, in one list:
SELECT c.code, i.name
FROM courses AS c
FULL JOIN instructors AS i ON i.id = c.instructor_id
ORDER BY c.code NULLS LAST, i.name;| codetext | nametext |
|---|---|
| CS101 | Hannah Okoye |
| CS201 | Marco Rossi |
| CS301 | Hannah Okoye |
| CS350 | Marco Rossi |
| CS401 | Lena Fischer |
| CS499 | null |
| HIST101 | Oliver Grant |
| MATH101 | Tomás Ortega |
| MATH201 | Yuki Tanaka |
| MATH301 | Tomás Ortega |
| PHYS101 | Amara Diallo |
| PHYS201 | Amara Diallo |
| null | Sofia Petrova |
13 rows: the 11 pairs, CS499 with no instructor (the senior thesis has nobody assigned yet), and Sofia Petrova with no course. FULL JOIN is the one you reach for when you're comparing two lists and want to see what's missing on either side.
counting what isn't there
padded rows are real rows, which matters the moment you count them:
SELECT i.name, count(*) AS rows, count(c.code) AS courses
FROM instructors AS i
LEFT JOIN courses AS c ON c.instructor_id = i.id
GROUP BY i.id
ORDER BY i.id;| nametext | rowsint8 | coursesint8 |
|---|---|---|
| Hannah Okoye | 2 | 2 |
| Marco Rossi | 2 | 2 |
| Lena Fischer | 1 | 1 |
| Tomás Ortega | 2 | 2 |
| Yuki Tanaka | 1 | 1 |
| Amara Diallo | 2 | 2 |
| Oliver Grant | 1 | 1 |
| Sofia Petrova | 1 | 0 |
count(*) counts rows, and Sofia has one: the padded one. count(c.code) counts values that aren't NULL, and she has none. so to count matches through a LEFT JOIN, count a column from the right table, ideally its primary key. (aggregate functions has the three counts side by side.) sum has the opposite problem: over nothing but NULLs it's NULL, not 0, so wrap it in coalesce(sum(...), 0) when a zero is what you mean.
the WHERE that undoes a LEFT JOIN
now the classic. same LEFT JOIN, but only the 4-credit courses:
SELECT i.name, c.code, c.credits
FROM instructors AS i
LEFT JOIN courses AS c ON c.instructor_id = i.id
WHERE c.credits >= 4;step through it. the stepper's tables are the real intermediate results, captured from the same postgres the page runs:
- 1SELECT i.name, c.code, c.credits
- 2FROM instructors AS i
- 3LEFT JOIN courses AS c ON c.instructor_id = i.id
- 4WHERE c.credits >= 4
- 5ORDER BY i.id, c.code;
| nametext | codetext | creditsint2 |
|---|---|---|
| Hannah Okoye | CS101 | 4 |
| Hannah Okoye | CS301 | 4 |
| Marco Rossi | CS201 | 4 |
| Marco Rossi | CS350 | 4 |
| Lena Fischer | CS401 | 4 |
| Tomás Ortega | MATH101 | 4 |
| Tomás Ortega | MATH301 | 3 |
| Yuki Tanaka | MATH201 | 3 |
| Amara Diallo | PHYS101 | 4 |
| Amara Diallo | PHYS201 | 4 |
| Oliver Grant | HIST101 | 3 |
| Sofia Petrova | null | null |
the LEFT JOIN keeps all 8 instructors: 11 matched pairs, plus Sofia Petrova, who teaches nothing, padded with NULLs. 12 rows.
the query that produced this table
NULL >= 4 isn't true.the fix is to move the condition into ON. ON decides which pairs count as a match, before the padding. a course with 3 credits then simply isn't a match, and an instructor with no 4-credit course gets the padded row instead of disappearing:
SELECT i.name, c.code, c.credits
FROM instructors AS i
LEFT JOIN courses AS c ON c.instructor_id = i.id
WHERE c.credits >= 4
ORDER BY i.id, c.code;| nametext | codetext | creditsint2 |
|---|---|---|
| Hannah Okoye | CS101 | 4 |
| Hannah Okoye | CS301 | 4 |
| Marco Rossi | CS201 | 4 |
| Marco Rossi | CS350 | 4 |
| Lena Fischer | CS401 | 4 |
| Tomás Ortega | MATH101 | 4 |
| Amara Diallo | PHYS101 | 4 |
| Amara Diallo | PHYS201 | 4 |
SELECT i.name, c.code, c.credits
FROM instructors AS i
LEFT JOIN courses AS c ON c.instructor_id = i.id AND c.credits >= 4
ORDER BY i.id, c.code;| nametext | codetext | creditsint2 |
|---|---|---|
| Hannah Okoye | CS101 | 4 |
| Hannah Okoye | CS301 | 4 |
| Marco Rossi | CS201 | 4 |
| Marco Rossi | CS350 | 4 |
| Lena Fischer | CS401 | 4 |
| Tomás Ortega | MATH101 | 4 |
| Yuki Tanaka | null | null |
| Amara Diallo | PHYS101 | 4 |
| Amara Diallo | PHYS201 | 4 |
| Oliver Grant | null | null |
| Sofia Petrova | null | null |
postgres can tell that the WHERE undid the outer join. compare the two plans: the second one doesn't do an outer join at all.
EXPLAIN (COSTS OFF)
SELECT i.name, c.code FROM instructors AS i
LEFT JOIN courses AS c ON c.instructor_id = i.id;
EXPLAIN (COSTS OFF)
SELECT i.name, c.code FROM instructors AS i
LEFT JOIN courses AS c ON c.instructor_id = i.id
WHERE c.credits >= 4;the first is your LEFT JOIN (Hash Right Join is the same join with its two inputs swapped: the planner chose to hash instructors). the second is a plain Hash Join. c.credits >= 4 can never be true for a padded row, so postgres drops the padding step it would only throw away. it didn't change your answer. it just shows you what your query already meant.
the rule of thumb: in a LEFT JOIN, conditions on the right table go in ON, conditions on the left table go in WHERE. a condition on the left table in ON doesn't filter anything, by the way: a left row that fails it just comes back padded.
there's one WHERE on the right table that's on purpose: WHERE c.code IS NULL. it keeps only the padded rows, which is "instructors who teach nothing". that's an anti join, and EXISTS, semi joins and anti joins has the cleaner way to write it.
a LEFT JOIN, then an inner one
the other way to lose your padded rows: join something else after them with a plain JOIN. "how many students does each instructor teach?" goes instructors → courses → enrollments:
- 1SELECT i.name, count(e.student_id) AS students
- 2FROM instructors AS i
- 3LEFT JOIN courses AS c ON c.instructor_id = i.id
- 4JOIN enrollments AS e ON e.course_code = c.code
- 5GROUP BY i.id
- 6ORDER BY i.id;
| nametext | studentsint8 |
|---|---|
| Hannah Okoye | 28 |
| Marco Rossi | 36 |
| Lena Fischer | 13 |
| Tomás Ortega | 27 |
| Yuki Tanaka | 16 |
| Amara Diallo | 33 |
| Oliver Grant | 11 |
LEFT JOIN, then an inner JOIN: Sofia's padded row has no course code to match an enrollment, so the inner join drops her. 7 instructors.
JOIN needs a course code to match an enrollment, and Sofia's padded row has NULL there, so she drops out. a second LEFT JOIN keeps her, with 0 students.the padded row reaches the second join like any other row, with NULL in c.code, and an inner join on e.course_code = c.code has nothing to match it with. once a table hangs off an outer join, every join after it on that path usually has to be outer too. (or nest them, LEFT JOIN (courses AS c JOIN enrollments AS e ON e.course_code = c.code) ON c.instructor_id = i.id, which says "instructors, and their courses that have students".)
this is also why outer joins can't be reordered as freely as inner joins: (a LEFT JOIN b) JOIN c and a LEFT JOIN (b JOIN c) are different questions with different answers.
keylines
- LEFT JOIN: every matched pair, plus each unmatched left row once, padded with NULLs.
- RIGHT is LEFT with the tables swapped. FULL keeps leftovers from both sides.
- count a right-table column, not
*, to count matches through a LEFT JOIN. - conditions on the right table go in ON. in WHERE they drop the padded rows.
- after an outer join, a plain JOIN on the padded side undoes it.