← Back to SQL, page 2

sql-0032 · Medium mid

Why can AVG(marks) differ from SUM(marks) / COUNT(*)? Which one is correct?

AVG ignores NULLs, so it divides the sum by the number of non-NULL values. SUM(marks) / COUNT(*) divides by all rows, treating missing marks as if they were zero. Which is correct depends on meaning: a mark that is still pending should usually be excluded, while a missed exam scored as zero should be stored as 0, not NULL.

Full answer

All aggregate functions except COUNT(*) skip NULLs. For AVG, that changes the denominator.

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 Statistics course has two enrolments, one marked 80 and one with no mark yet:

sql
SELECT AVG(marks)                    AS avg_marks,
       SUM(marks) * 1.0 / COUNT(*)   AS sum_over_rows,
       SUM(marks) / COUNT(*)         AS integer_division
FROM enrolments
WHERE course_id = 30;
avg_markssum_over_rowsinteger_division
80.040.040

AVG(marks) is 80, because only one value exists. Dividing the sum by COUNT(*) gives 40, as though the missing mark were zero. And SUM(marks) / COUNT(*) with two integers also performs integer division, a second trap hiding in the same expression (here 80 / 2 happens to divide evenly).

Neither number is wrong by itself; they answer different questions. "What is the average of the marks entered so far?" is 80. "What is the average if we count a missing mark as zero?" is 40. The data model should make the meaning clear. If a student skipped the exam and the rule is that they score zero, store 0. Keep NULL for "not known yet".

If you do want missing values counted as zero, say so explicitly rather than relying on the division:

sql
SELECT AVG(COALESCE(marks, 0)) AS avg_missing_as_zero
FROM enrolments
WHERE course_id = 30;
avg_missing_as_zero
40.0
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 AVG(marks)                    AS avg_marks,
       SUM(marks) * 1.0 / COUNT(*)   AS sum_over_rows,
       SUM(marks) / COUNT(*)         AS integer_division
FROM enrolments
WHERE course_id = 30;

SELECT AVG(COALESCE(marks, 0)) AS avg_missing_as_zero
FROM enrolments
WHERE course_id = 30;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Aggregates other than COUNT(*) ignore NULL
  • AVG divides by the count of non-NULL values
  • SUM / COUNT(*) treats NULL as zero and may do integer division
  • Store 0 for a real zero and NULL for unknown

Common mistakes

  • Recomputing an average by hand and getting a different answer than AVG
  • Using NULL to mean zero in the data

Follow-ups

  • What does AVG return when every value is NULL?
  • How would you show the average per course with a count of missing marks?

Tips

  • Ask what NULL means in this column before choosing the formula