Keeping rows with no partner
After this: Keep every row of the left table with LEFT JOIN, find the unmatched ones, and count with COUNT(column).
On this page · 5 sections · 4 checks
The school has six students, but the list of who takes what had five names. Eve was missing, because she has not chosen any course yet. For a list of who takes what, that is fine. For a register of every student, it is wrong: Eve is a student and must be on it. This lesson keeps her.
Why Eve disappears
A plain JOIN keeps only pairs that pass the ON test. Eve's id is 5, and no enrolment row has student_id 5. So when students are joined to enrolments, Eve's row finds no partner and is left out:
Keeping every row on the left
What you want is: every student, with their enrolments beside them, and for a student who has none, one row anyway, with the enrolment columns left empty. In SQL, a missing value is NULL, which you met in "Reading data".
That is a . It keeps every row of the table on its left, the one named before LEFT JOIN, whether or not it finds a partner.
- Ana has three enrolments: three rows, as before.
- Ben, Chloe and Dev have two each, and Femi one: seven more rows.
- Eve has none: one row, with NULL in every enrolment column.
That is 3 + 7 = 10 matched rows, as in the plain join, plus Eve's one: 11.
Step through the full rows and find Eve:
SELECT s.name, e.course_id, e.markFROM students AS sLEFT JOIN enrolments AS e ON e.student_id = s.idORDER BY s.id, e.course_id;students · 6 rows
| id | name | year | city |
|---|---|---|---|
| 1 | Ana | 2 | Leeds |
| 2 | Ben | 1 | York |
| 3 | Chloe | 2 | Leeds |
| 4 | Dev | 3 | Hull |
| 5 | Eve | 1 | York |
| 6 | Femi | 3 | Leeds |
enrolments · 10 rows
| student_id | course_id | mark |
|---|---|---|
| 1 | 1 | 72 |
| 1 | 2 | 65 |
| 1 | 3 | 80 |
| 2 | 1 | 58 |
| 2 | 4 | 90 |
| 3 | 1 | 85 |
| 3 | 3 | NULL |
| 4 | 2 | 47 |
| 4 | 4 | 76 |
| 6 | 1 | 91 |
Press Run it, then step through the query one clause at a time, in the order SQLite works.
- rows
- 0
- columns
- 0
At the LEFT JOIN step, ten rows have both halves filled. Eve's row has her student columns and NULL in student_id, course_id and mark of enrolments. The only change from the plain join is the word LEFT.
Finding the rows with no partner
Eve's row is the only one whose enrolment columns are NULL. So a LEFT JOIN followed by an IS NULL test finds every student with no enrolment:
Test a column that can be NULL only when no partner was found. e.student_id is a good choice: every real enrolment has one. e.mark is a bad choice, because Chloe's real History enrolment has a NULL mark too.
The same pattern, turned around, finds the courses that nobody takes. Now courses is on the left:
Counting with a LEFT JOIN
How many courses does each student take, Eve included? Join with LEFT JOIN, group by student and count:
Eve gets 0. That depends on what is counted. COUNT(e.course_id) counts the values in that column that are not NULL, as "Counting and grouping" showed. Eve's group has one row, and its course_id is NULL, so the count is 0.
Now count with COUNT(*) instead:
Eve gets 1. COUNT(*) counts rows, and her NULL-filled row is still a row. Every other student's count is the same in both queries. So to count partners after a LEFT JOIN, count a column of the right-hand table that is never NULL in a real row, such as e.course_id, not *. A column such as e.mark would be wrong too: Chloe's unmarked History row would not be counted.
The stepped version below keeps only students with fewer than two courses. Watch the groups at the GROUP BY step: Eve's group has one row, and HAVING still sees a count of 0 for her.
SELECT s.name, COUNT(e.course_id) AS coursesFROM students AS sLEFT JOIN enrolments AS e ON e.student_id = s.idGROUP BY s.id, s.nameHAVING COUNT(e.course_id) < 2ORDER BY s.name;students · 6 rows
| id | name | year | city |
|---|---|---|---|
| 1 | Ana | 2 | Leeds |
| 2 | Ben | 1 | York |
| 3 | Chloe | 2 | Leeds |
| 4 | Dev | 3 | Hull |
| 5 | Eve | 1 | York |
| 6 | Femi | 3 | Leeds |
enrolments · 10 rows
| student_id | course_id | mark |
|---|---|---|
| 1 | 1 | 72 |
| 1 | 2 | 65 |
| 1 | 3 | 80 |
| 2 | 1 | 58 |
| 2 | 4 | 90 |
| 3 | 1 | 85 |
| 3 | 3 | NULL |
| 4 | 2 | 47 |
| 4 | 4 | 76 |
| 6 | 1 | 91 |
Press Run it, then step through the query one clause at a time, in the order SQLite works.
- rows
- 0
- columns
- 0
Check yourself
You want a list of every student with their courses, and a student who takes nothing must still be on it. Which join do you write after FROM students?
With a plain JOIN, students and enrolments give 10 rows. How many rows does this LEFT JOIN count?
SELECT COUNT(*) AS row_countFROM students AS sLEFT JOIN enrolments AS e ON e.student_id = s.id;This query counts with COUNT(*) instead of COUNT(e.course_id). What number does it give for Eve?
SELECT s.name, COUNT(*) AS coursesFROM students AS sLEFT JOIN enrolments AS e ON e.student_id = s.idGROUP BY s.id, s.nameORDER BY s.id;You want the courses that nobody takes. Which query gives them?
- Say why a plain join drops Eve: she has no enrolment, so her row finds no partner.
- Keep every row of the left table with LEFT JOIN; a row with no partner gets NULL in the other table's columns.
- Find the rows with no partner with LEFT JOIN and IS NULL on a column that is NULL only when nothing matched.
- Count with COUNT(column) after a LEFT JOIN: COUNT(*) gives Eve 1, COUNT(e.course_id) gives her 0.