Joins on one deliberately messy dataset
Pair students with enrolments, keep the missing matches and discover why counting joined rows is different from counting people. Querying · Databases.
You have six students and seven enrolments. Your report shows five rows. Someone changes one word in the query and it shows eight. Nobody enrolled overnight. The join changed which missing matches survive, and a student with two courses was already taking two places in the report. Let's watch each row arrive.
Match pairs before you count people
Read the first frame below. The left table holds students; the right holds enrolments. For this lesson, enrolments is an imported legacy table with no foreign-key constraint. That is why student id 9 can appear without a student, and why one enrolment can have no student key. An enforced constraint would reject the missing non-NULL reference; we keep it here to see what a query does with imperfect data.
The condition is students.id = enrolments.student_id. It compares ids, never names or cities. There are two students named Kavya, with ids 1 and 5. Sana's city is NULL, but her id is 3. A missing city tells us nothing about whether her id matches an enrolment.
Start with INNER JOIN. The probe takes one student at a time and finds every matching enrolment. The key cells flash as they are compared, a line links each pair, and the joined row moves into the result table. Play it, or step through at your own pace.
INNER: one row for each pair
Read the steps as text
- INNER JOIN of students with enrolments on students.id = enrolments.student_id.
- students 1 has key 1; enrolments 10, 11 have it. 2 joined rows added.
- students 2 has key 2; enrolments 12 has it. 1 joined row added.
- students 3 has key 3; no enrolments row has it. INNER JOIN drops it.
- students 4 has key 4; enrolments 13, 16 have it. 2 joined rows added.
- students 5 has key 5; no enrolments row has it. INNER JOIN drops it.
- students 6 has key 6; no enrolments row has it. INNER JOIN drops it.
- enrolments 14 has key 9; no students row has it. INNER JOIN drops it.
- enrolments 15 has a NULL key, and NULL equals nothing. INNER JOIN drops it.
- INNER JOIN returns 5 rows: 5 paired.
Student 1 pairs with enrolments 10 and 11. Dev pairs with 13 and 16. Imran pairs with 12. Those are 5 pairs, not five different students. Neither Kavya's repeated name nor Sana's missing city changes the condition. The other students add nothing because INNER JOIN only keeps pairs.
SELECT s.id, s.name, s.city, e.id, e.student_id, e.course
FROM students AS s
INNER JOIN enrolments AS e ON s.id = e.student_id;The animation is a way to reason about the result, not a claim about the database's execution plan. We show students in input order, their partners in enrolment order, then unpaired enrolments. SQL promises no row order without ORDER BY.
Keep students who have no match
Now change INNER to LEFT. The tables and equality stay the same. As it plays, look for the students whose ids never occur in student_id. Each of them needs a place in a report that promises to include every student.
LEFT: keep every student
Read the steps as text
- LEFT JOIN of students with enrolments on students.id = enrolments.student_id.
- students 1 has key 1; enrolments 10, 11 have it. 2 joined rows added.
- students 2 has key 2; enrolments 12 has it. 1 joined row added.
- students 3 has key 3; no enrolments row has it. LEFT JOIN keeps it with NULLs for enrolments.
- students 4 has key 4; enrolments 13, 16 have it. 2 joined rows added.
- students 5 has key 5; no enrolments row has it. LEFT JOIN keeps it with NULLs for enrolments.
- students 6 has key 6; no enrolments row has it. LEFT JOIN keeps it with NULLs for enrolments.
- enrolments 14 has key 9; no students row has it. LEFT JOIN drops it.
- enrolments 15 has a NULL key, and NULL equals nothing. LEFT JOIN drops it.
- LEFT JOIN returns 8 rows: 5 paired and 3 kept with NULLs for enrolments.
The 5 pairs remain. Sana, the second Kavya and Leo each add one row with NULL in every enrolment column. That gives 8 rows. LEFT preserves all students, but it does not promise exactly six rows: two matching courses still make two pairs. The NULLs in a padded row mean that this query found no matching enrolment. They do not insert a new record into either source table.
Keep enrolments instead
RIGHT promises to preserve the enrolment table, which sits on the right of this query. It keeps the same pairs and adds the imported rows that could not pair. Watch enrolment 14, whose key is 9, and enrolment 15, whose key is NULL.
RIGHT: keep every enrolment
Read the steps as text
- RIGHT JOIN of students with enrolments on students.id = enrolments.student_id.
- students 1 has key 1; enrolments 10, 11 have it. 2 joined rows added.
- students 2 has key 2; enrolments 12 has it. 1 joined row added.
- students 3 has key 3; no enrolments row has it. RIGHT JOIN drops it.
- students 4 has key 4; enrolments 13, 16 have it. 2 joined rows added.
- students 5 has key 5; no enrolments row has it. RIGHT JOIN drops it.
- students 6 has key 6; no enrolments row has it. RIGHT JOIN drops it.
- enrolments 14 has key 9; no students row has it. RIGHT JOIN keeps it with NULLs for students.
- enrolments 15 has a NULL key, and NULL equals nothing. RIGHT JOIN keeps it with NULLs for students.
- RIGHT JOIN returns 7 rows: 5 paired and 2 kept with NULLs for students.
Both survive with NULL student columns. The result has 7 rows. Missing key 9 and unknown key NULL are different data problems, yet neither finds an equal student id. Ordinary equality involving NULL is unknown, so it never establishes a pair. RIGHT does not rescue the unmatched students: preserving students was LEFT's promise.
Keep missing matches on both sides
FULL keeps all pairs, all unmatched students and all unmatched enrolments. As the final frame plays, watch the rows fall into those three groups. Keep the groups separate: a row padded with NULLs is not sent back through the equality test to find another partner.
FULL: keep both kinds of missing match
Read the steps as text
- FULL JOIN of students with enrolments on students.id = enrolments.student_id.
- students 1 has key 1; enrolments 10, 11 have it. 2 joined rows added.
- students 2 has key 2; enrolments 12 has it. 1 joined row added.
- students 3 has key 3; no enrolments row has it. FULL JOIN keeps it with NULLs for enrolments.
- students 4 has key 4; enrolments 13, 16 have it. 2 joined rows added.
- students 5 has key 5; no enrolments row has it. FULL JOIN keeps it with NULLs for enrolments.
- students 6 has key 6; no enrolments row has it. FULL JOIN keeps it with NULLs for enrolments.
- enrolments 14 has key 9; no students row has it. FULL JOIN keeps it with NULLs for students.
- enrolments 15 has a NULL key, and NULL equals nothing. FULL JOIN keeps it with NULLs for students.
- FULL JOIN returns 10 rows: 5 paired, 3 kept with NULLs for enrolments and 2 kept with NULLs for students.
The total is 5 paired rows plus 3 student-only rows plus 2 enrolment-only rows: 10. FULL has not made the orphan enrolments valid. It has made them visible beside the students who have no enrolments, so you can investigate both gaps.
A MySQL workaround with a condition
MySQL has LEFT and RIGHT joins but no native FULL OUTER JOIN syntax. For these tables, selecting all six columns lets us combine the LEFT result with the RIGHT result using UNION. The paired rows appear in both branches. UNION removes equal result rows, leaving one copy of each pair and both kinds of unmatched row.
LEFT UNION RIGHT: check what disappears
Read the steps as text
- UNION of a LEFT JOIN and a RIGHT JOIN of students with enrolments: the first join's rows come first, then the new rows of the second.
- LEFT JOIN, scanned in one pass: every students row is probed and then each enrolments row that found no partner. It keeps 8 rows.
- RIGHT JOIN, scanned in one pass: every students row is probed and then each enrolments row that found no partner. It keeps 7 rows.
- UNION keeps the first copy of each distinct row and drops 5 repeated rows. NULL counts as equal to NULL here.
- UNION returns 10 distinct rows: 8 from the first join and 2 added by the second.
SELECT s.id, s.name, s.city, e.id, e.student_id, e.course
FROM students s LEFT JOIN enrolments e ON s.id = e.student_id
UNION
SELECT s.id, s.name, s.city, e.id, e.student_id, e.course
FROM students s RIGHT JOIN enrolments e ON s.id = e.student_id;That yields 10 rows here. The ids make each distinct pair distinguishable. If you select only course, different enrolments can look identical and UNION can erase valid duplicates. Replacing it with UNION ALL also fails here: it keeps each matched pair twice. A general duplicate-preserving workaround needs a different construction; this scene proves only the stated projection.
Fresh tables, same join
Now pair library members with imported loans using members.id = loans.member_id. Watch each member and loan go through the same FULL JOIN: every pair, member-only row and loan-only row arrives as before. The quiz asks the same questions afterwards.
Your move: members and loans
Read the steps as text
- FULL JOIN of members with loans on members.id = loans.member_id.
- members 21 has key 21; loans 31, 32 have it. 2 joined rows added.
- members 22 has key 22; no loans row has it. FULL JOIN keeps it with NULLs for loans.
- members 23 has key 23; no loans row has it. FULL JOIN keeps it with NULLs for loans.
- loans 33 has key 99; no members row has it. FULL JOIN keeps it with NULLs for members.
- loans 34 has a NULL key, and NULL equals nothing. FULL JOIN keeps it with NULLs for members.
- FULL JOIN returns 6 rows: 2 paired, 2 kept with NULLs for loans and 2 kept with NULLs for members.
Explain the final 6 rows as pairs, member-only rows and loan-only rows. If a total looks surprising, inspect one group rather than guessing a join name.
Recap
- Count pairs before counting result rows; a repeated person can be correct.
- LEFT, RIGHT and FULL add unmatched rows according to their preservation promise.
- Missing keys never pair through ordinary equality, and UNION compares the selected values.
Two places a NULL can come from
Sana's city was already NULL in the source. The NULL enrolment id beside Sana in a LEFT result was introduced because no enrolment matched. A result can contain both at once. Inspecting the non-NULL source row id on the missing side helps tell a padded row from a real row with a nullable field.
The next query lesson moves a condition between ON and WHERE. ON decides which rows pair; WHERE filters the completed join result. A right-side equality in WHERE rejects padded NULL rows, which can undo LEFT's preservation. That is a new filter step, not a different definition of LEFT JOIN.
For another way to trace relationships between records, explore hash maps in Fundamentals. A map lookup helps find a key; the join still has to keep every matching row, including multiple partners.