← Back to SQL, page 17

sql-0334 · Easy fresher

A grading query gives every passing student a C. What is wrong with the CASE?

CASE checks its WHEN branches in order and returns the first true one. Since marks >= 50 is listed first, it is true for every passing mark and the A and B branches are never reached. List the highest thresholds first.

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 query as written:

sql
SELECT student_id, course_id, marks,
  CASE WHEN marks >= 50 THEN 'C' WHEN marks >= 70 THEN 'B' WHEN marks >= 85 THEN 'A' ELSE 'F' END AS grade
FROM enrolments ORDER BY student_id, course_id;
student_idcourse_idmarksgrade
11088C
12092C
21075C
230NULLF
31064C
32071C
33080C
52055C

A mark of 92 satisfies marks >= 50, so the CASE stops there and returns C. The NULL mark falls through to ELSE and shows F, which is a second bug: an unmarked enrolment is not a failure.

A corrected version:

sql
SELECT student_id, course_id, marks,
  CASE WHEN marks IS NULL THEN 'pending' WHEN marks >= 85 THEN 'A' WHEN marks >= 70 THEN 'B' WHEN marks >= 50 THEN 'C' ELSE 'F' END AS grade
FROM enrolments ORDER BY student_id, course_id;
student_idcourse_idmarksgrade
11088A
12092A
21075B
230NULLpending
31064C
32071B
33080B
52055C

Ordering thresholds from high to low makes each branch reachable, and an explicit NULL branch labels the missing mark. Marks of 88 and 92 now show A, 75, 71 and 80 show B, 64 and 55 show C.

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,
  CASE WHEN marks >= 50 THEN 'C' WHEN marks >= 70 THEN 'B' WHEN marks >= 85 THEN 'A' ELSE 'F' END AS grade
FROM enrolments ORDER BY student_id, course_id;

SELECT student_id, course_id, marks,
  CASE WHEN marks IS NULL THEN 'pending' WHEN marks >= 85 THEN 'A' WHEN marks >= 70 THEN 'B' WHEN marks >= 50 THEN 'C' ELSE 'F' END AS grade
FROM enrolments ORDER BY student_id, course_id;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • CASE returns the first true branch
  • Order range checks from most to least specific
  • Handle NULL explicitly before ELSE

Common mistakes

  • Overlapping ranges in the wrong order
  • Letting NULL fall into ELSE

Follow-ups

  • How would you store grade thresholds in a table instead?
  • How would you count students per grade?

Tips

  • Mention the NULL branch too