Full answer
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);The overall average treats every marked enrolment as one vote:
SELECT ROUND(AVG(marks), 2) AS overall_avg, COUNT(marks) AS marked_papers FROM enrolments;| overall_avg | marked_papers |
|---|---|
| 75.0 | 7 |
The per-student averages, then their average, treat every student as one vote:
SELECT student_id, ROUND(AVG(marks), 2) AS student_avg, COUNT(marks) AS papers
FROM enrolments
WHERE marks IS NOT NULL
GROUP BY student_id
ORDER BY student_id;| student_id | student_avg | papers |
|---|---|---|
| 1 | 90.0 | 2 |
| 2 | 75.0 | 1 |
| 3 | 71.67 | 3 |
| 5 | 55.0 | 1 |
SELECT ROUND(AVG(student_avg), 2) AS avg_of_student_avgs
FROM (SELECT student_id, AVG(marks) AS student_avg
FROM enrolments WHERE marks IS NOT NULL
GROUP BY student_id);| avg_of_student_avgs |
|---|
| 72.92 |
The overall figure is 75.0 and the student-weighted figure is 72.92. Chen has three marked courses, so in the overall average his marks count three times; Ben and Eli each count once.
Neither number is wrong. A course coordinator asking "how did papers score on average?" wants the first. A mentor asking "how is a typical student doing?" wants the second. The mistake is mixing them, for example averaging department averages to get a company average salary. When the groups have different sizes, an average of averages is only equal to the overall average by coincidence.
If you have pre-computed averages and counts, you can recover the overall figure as a weighted average: SUM(avg * n) / SUM(n).
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 ROUND(AVG(marks), 2) AS overall_avg, COUNT(marks) AS marked_papers FROM enrolments;
SELECT student_id, ROUND(AVG(marks), 2) AS student_avg, COUNT(marks) AS papers
FROM enrolments
WHERE marks IS NOT NULL
GROUP BY student_id
ORDER BY student_id;
SELECT ROUND(AVG(student_avg), 2) AS avg_of_student_avgs
FROM (SELECT student_id, AVG(marks) AS student_avg
FROM enrolments WHERE marks IS NOT NULL
GROUP BY student_id);Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Overall AVG weights each row equally
- Average of group averages weights each group equally
- They match only when every group has the same size
- Recover the overall average with SUM(avg * n) / SUM(n)
Common mistakes
- Averaging department averages to report a company average
- Presenting either number without saying which weighting it uses
Follow-ups
- How would you compute a credit-weighted average mark?
- What happens to both numbers if NULL marks are included?
Tips
- State which question each number answers