Full answer
sql
CREATE TABLE students (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
city TEXT,
year INTEGER
);
CREATE TABLE courses (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
credits INTEGER
);
CREATE TABLE enrolments (
student_id INTEGER REFERENCES students(id),
course_id INTEGER REFERENCES courses(id),
marks INTEGER,
PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
(1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
(3,10,64),(3,20,71),(3,30,80),(5,20,55);sql
SELECT c.title, COUNT(*) AS students
FROM courses c
LEFT JOIN enrolments e ON e.course_id = c.id
GROUP BY c.id, c.title
ORDER BY c.title;| title | students |
|---|---|
| Algorithms | 3 |
| Databases | 3 |
| Ethics | 1 |
| Statistics | 2 |
Ethics has nobody enrolled, but the LEFT JOIN still produced one row for it, filled with NULLs on the right side, so that the course is not lost. COUNT(*) counts rows, and there is one row.
Count something that only exists when there is a real match:
sql
SELECT c.title, COUNT(e.student_id) AS students
FROM courses c
LEFT JOIN enrolments e ON e.course_id = c.id
GROUP BY c.id, c.title
ORDER BY c.title;| title | students |
|---|---|
| Algorithms | 3 |
| Databases | 3 |
| Ethics | 0 |
| Statistics | 2 |
COUNT(e.student_id) skips NULLs, so Ethics correctly gets 0. Use the right table's key or another NOT NULL column of the right table. Counting a nullable column such as marks would give the wrong answer for Statistics, where one enrolled student has no mark yet:
sql
SELECT c.title,
COUNT(e.student_id) AS students,
COUNT(e.marks) AS marked
FROM courses c
LEFT JOIN enrolments e ON e.course_id = c.id
GROUP BY c.id, c.title
ORDER BY c.title;| title | students | marked |
|---|---|---|
| Algorithms | 3 | 3 |
| Databases | 3 | 3 |
| Ethics | 0 | 0 |
| Statistics | 2 | 1 |
Statistics has two students but only one mark. Pick the column whose meaning matches the question: count enrolments with the key, count marked papers with marks.
sql
CREATE TABLE students (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
city TEXT,
year INTEGER
);
CREATE TABLE courses (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
credits INTEGER
);
CREATE TABLE enrolments (
student_id INTEGER REFERENCES students(id),
course_id INTEGER REFERENCES courses(id),
marks INTEGER,
PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
(1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
(3,10,64),(3,20,71),(3,30,80),(5,20,55);
SELECT c.title, COUNT(*) AS students
FROM courses c
LEFT JOIN enrolments e ON e.course_id = c.id
GROUP BY c.id, c.title
ORDER BY c.title;
SELECT c.title, COUNT(e.student_id) AS students
FROM courses c
LEFT JOIN enrolments e ON e.course_id = c.id
GROUP BY c.id, c.title
ORDER BY c.title;
SELECT c.title,
COUNT(e.student_id) AS students,
COUNT(e.marks) AS marked
FROM courses c
LEFT JOIN enrolments e ON e.course_id = c.id
GROUP BY c.id, c.title
ORDER BY c.title;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- A LEFT JOIN produces one NULL-filled row for unmatched left rows
- COUNT(*) counts that row; COUNT(right.key) does not
- Count a NOT NULL column of the right table for 'number of matches'
- COUNT(nullable column) answers a different question
Common mistakes
- Using COUNT(*) after a LEFT JOIN and reporting 1 for empty groups
- Counting a nullable column and undercounting real matches
Follow-ups
- How would you also show the average mark per course, blank for Ethics?
- Would an INNER JOIN fix it?
Tips
- Explain where the extra row comes from before giving the fix