← Back to SQL, page 25

sql-0486 · Medium mid

Build a histogram of marks in bands of 0-49, 50-69, 70-84 and 85-100, including bands with no students.

Define the bands in a VALUES list (or a small table) with lower and upper bounds, LEFT JOIN the marks on mark BETWEEN low AND high, and count matching marks per band. Starting from the band list ensures empty bands appear with a count of zero.

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
WITH bands(label, low, high) AS (VALUES ('0-49', 0, 49), ('50-69', 50, 69), ('70-84', 70, 84), ('85-100', 85, 100))
SELECT b.label, COUNT(e.marks) AS students
FROM bands b LEFT JOIN enrolments e ON e.marks BETWEEN b.low AND b.high
GROUP BY b.label, b.low ORDER BY b.low;
labelstudents
0-490
50-692
70-843
85-1002

No mark is below 50, yet the '0-49' band still appears with 0, because the query starts from the bands. COUNT(e.marks) counts only joined rows, so empty bands show zero rather than one. The unmarked enrolment has a NULL mark, matches no band and is left out; add a separate 'not marked' line if it should be reported. A CASE expression would also bucket the marks, but bands with no rows would then be missing.

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

WITH bands(label, low, high) AS (VALUES ('0-49', 0, 49), ('50-69', 50, 69), ('70-84', 70, 84), ('85-100', 85, 100))
SELECT b.label, COUNT(e.marks) AS students
FROM bands b LEFT JOIN enrolments e ON e.marks BETWEEN b.low AND b.high
GROUP BY b.label, b.low ORDER BY b.low;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Start from a band list for empty buckets
  • LEFT JOIN with BETWEEN bounds
  • COUNT the joined column, not *
  • NULL values match no band

Common mistakes

  • Bucketing with CASE and losing empty bands
  • COUNT(*) showing 1 for empty bands

Follow-ups

  • How would you make equal-width bands automatically?
  • How would you show percentages per band?

Tips

  • Explain why empty bands appear