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_id | course_id | marks | class_avg | flag |
|---|---|---|---|---|
| 1 | 10 | 88 | 75.0 | above |
| 1 | 20 | 92 | 75.0 | above |
| 2 | 10 | 75 | 75.0 | not above |
| 2 | 30 | NULL | 75.0 | no mark |
| 3 | 10 | 64 | 75.0 | not above |
| 3 | 20 | 71 | 75.0 | not above |
| 3 | 30 | 80 | 75.0 | above |
| 5 | 20 | 55 | 75.0 | not 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