← Back to SQL, page 14

sql-0266 · Easy fresher

A student table has a marks column. Write a query that returns each student's mark and the class average, and flags whether they are above it.

Use AVG(marks) OVER () to put the overall average on every row, then compare with CASE. A scalar subquery (SELECT AVG(marks) FROM ...) does the same. Both ignore NULL marks in the average.

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 student_id, course_id, marks,
       ROUND(AVG(marks) OVER (), 1) AS class_avg,
       CASE WHEN marks IS NULL THEN 'no mark'
            WHEN marks > AVG(marks) OVER () THEN 'above'
            ELSE 'not above' END AS flag
FROM enrolments ORDER BY student_id, course_id;
student_idcourse_idmarksclass_avgflag
1108875.0above
1209275.0above
2107575.0not above
230NULL75.0no mark
3106475.0not above
3207175.0not above
3308075.0above
5205575.0not above

The seven non-NULL marks average exactly 75.0; AVG skips the NULL, so it divides by seven, not eight. Ben's 75 in Databases equals the average and is therefore 'not above'.

Ben's NULL mark is labelled 'no mark' by testing for NULL first. Without that branch, NULL > 75 would be unknown, the CASE would fall through to ELSE, and the row would be labelled 'not above', which is misleading.

The scalar subquery form is equivalent:

sql
SELECT COUNT(*) AS above_avg FROM enrolments WHERE marks > (SELECT AVG(marks) FROM enrolments);
above_avg
3
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 student_id, course_id, marks,
       ROUND(AVG(marks) OVER (), 1) AS class_avg,
       CASE WHEN marks IS NULL THEN 'no mark'
            WHEN marks > AVG(marks) OVER () THEN 'above'
            ELSE 'not above' END AS flag
FROM enrolments ORDER BY student_id, course_id;

SELECT COUNT(*) AS above_avg FROM enrolments WHERE marks > (SELECT AVG(marks) FROM enrolments);

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • AVG(x) OVER () gives an overall average per row
  • A scalar subquery is an alternative
  • AVG ignores NULLs
  • Handle NULL before comparing

Common mistakes

  • Using GROUP BY and losing the per-student rows
  • Labelling NULL marks as below average

Follow-ups

  • How would you compare with the course average instead?
  • How would you show the difference from the average?

Tips

  • Handle the NULL row explicitly