Three join mistakes
After this: Recognise a forgotten ON, a WHERE that undoes a LEFT JOIN, and a count that counts the empty row.
On this page · 5 sections · 4 checks
A join that is written wrong rarely gives an error. It gives rows, and the rows look like an answer. That is what makes join mistakes costly: nothing stops you. This lesson shows the three mistakes people make most often, so that you can spot each one by the numbers it gives.
Mistake 1: the forgotten ON
The ON test says which rows belong together. Leave it out and SQLite has nothing to test, so every pair passes: each student with each course.
- There are 6 students and 5 courses.
- Ana is paired with all 5 courses, Ben with all 5, and so on.
- 6 times 5 is 30 pairs, and none of them says who takes what.
Pairing every row of one table with every row of another is called a . Sometimes you want one, for example to list every student with every possible course on a planning sheet. Usually it is a mistake. The sign is a row count that is two table sizes multiplied, far more rows than there are real pairs.
An older way to write a join puts a comma between the tables, FROM students, courses, and the matching test in WHERE. Forget that WHERE and you get the same 30 rows.
Mistake 2: a WHERE that undoes a LEFT JOIN
You want every student, with only their marks above 80. A first try adds a WHERE to the LEFT JOIN from lesson 4. Step through it:
SELECT s.name, e.markFROM students AS sLEFT JOIN enrolments AS e ON e.student_id = s.idWHERE e.mark > 80ORDER 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
The LEFT JOIN makes 11 rows, Eve's included. Then WHERE tests e.mark > 80 on every row. Eve's mark is NULL, and NULL compared with anything is not true, so her row fails. So do all the marks of 80 or less. Three rows are left: Ben 90, Chloe 85, Femi 91. The LEFT JOIN kept Eve, and the WHERE removed her again.
The fix is to put the test in the ON line. Then it decides which enrolments count as partners, and a student with no such partner still keeps one row, with NULL:
Six rows: Ben, Chloe and Femi with their marks, and Ana, Dev and Eve with NULL.
The rule: after a LEFT JOIN, a WHERE test on a column of the right-hand table removes the rows that had no partner. If you want to keep them, put that test in ON. A test on the left-hand table, such as s.city = 'Leeds', belongs in WHERE as usual.
Mistake 3: counting the empty row
Lesson 4 showed this one with Eve. Here it is with courses. The course report counts students per course, Music included:
Music shows 0 students and an average mark of NULL. That is right: AVG of no marks has no value. Now the same count with COUNT(*):
Music now has 1 student, and nobody takes it. The LEFT JOIN gave Music one row, NULL-filled, and COUNT(*) counts rows. The other courses are unchanged, which is why this mistake is hard to see: only the courses with no partner are wrong.
Three questions to ask of any join
- Is there an ON for every JOIN? If the row count is two table sizes multiplied, one is missing.
- After a LEFT JOIN, does a WHERE test a column of the right-hand table? If so, the rows with no partner are gone.
- After a LEFT JOIN, does a count use
*? Count a column of the right-hand table that is never NULL in a real row, such as its key, instead.
Check yourself
SELECT COUNT(*) FROM students JOIN courses; gives 30. Where does 30 come from?
A LEFT JOIN of every student, then a WHERE on the mark. How many rows come back?
SELECT s.name, e.markFROM students AS sLEFT JOIN enrolments AS e ON e.student_id = s.idWHERE e.mark > 80ORDER BY s.name;You want all six students, each with their marks above 80, and NULL for a student who has none. How do you change the query above?
Students per course, counted with COUNT(*). What does it give for Music, which nobody takes?
SELECT c.title, COUNT(*) AS studentsFROM courses AS cLEFT JOIN enrolments AS e ON e.course_id = c.idGROUP BY c.id, c.titleORDER BY c.id;- Spot a forgotten ON: every row is paired with every row, and 6 students times 5 courses gives 30.
- Say why a WHERE test on the right-hand table removes the rows a LEFT JOIN kept, and move that test into ON to keep them.
- Say why COUNT(*) gives Music 1 student after a LEFT JOIN, and count a column of the right-hand table instead.
- Ask three questions of any join: is there an ON for every JOIN, does WHERE undo a LEFT JOIN, does the count use *?