← Back to SQL, page 4

sql-0076 · Hard senior

Why is the average of per-student averages different from the overall average mark, and which should a report use?

AVG over all enrolments weights each mark equally, so students with more courses count more. Averaging each student's average first gives each student equal weight. They are different questions: 'average mark across all papers' versus 'average of how students perform'. Pick the one the report means and label it.

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);

The overall average treats every marked enrolment as one vote:

sql
SELECT ROUND(AVG(marks), 2) AS overall_avg, COUNT(marks) AS marked_papers FROM enrolments;
overall_avgmarked_papers
75.07

The per-student averages, then their average, treat every student as one vote:

sql
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_idstudent_avgpapers
190.02
275.01
371.673
555.01
sql
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).

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 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