← Back to SQL, page 5

sql-0089 · Easy fresher

Write a query showing how many students there are per city and year, with the busiest combination first.

Group by both columns, count rows, and order by the count descending with the grouping columns as tie-breakers. Each output row is one distinct (city, year) combination. Rows with NULL in either column form their own combination.

Full answer

Grouping by several columns makes one group per distinct combination of their values.

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 city, year, COUNT(*) AS students
FROM students
GROUP BY city, year
ORDER BY COUNT(*) DESC, city, year;
cityyearstudents
Leeds22
Pune12
NULL31

Asha and Chen share (Pune, 1); Ben and Eli share (Leeds, 2). Dara has no city, so (NULL, 3) is its own group.

ORDER BY can sort on the aggregate, even though it was not given an alias, because ORDER BY runs after grouping. The two extra sort keys break the tie between the groups with 2 students so the order is repeatable.

If the question were "per city, with a breakdown by year", the shape is the same; the order of columns in GROUP BY does not change which groups exist, only the ORDER BY decides how they are presented.

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 city, year, COUNT(*) AS students
FROM students
GROUP BY city, year
ORDER BY COUNT(*) DESC, city, year;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • GROUP BY a, b gives one group per distinct combination
  • Column order in GROUP BY does not change the groups
  • ORDER BY can use aggregates
  • Add tie-breakers for a repeatable order

Common mistakes

  • Thinking GROUP BY a, b means 'group by a, then by b separately'
  • Sorting only by the count and getting ties in random order

Follow-ups

  • How would you show only combinations with at least two students?
  • How would you add a subtotal per city?

Tips

  • Explain what a single output row represents