Skip to the content

Not on the roster

Problem

The vice principal wants to know how many students each course could still be offered to: the ones who aren't taking it. Taking a course means being enrolled in it, with or without a grade.

Show each course's name and how many of the school's students aren't taking it, in that order. Every student counts, including anyone who isn't taking any course. A course nobody takes is missing the whole school, and a course everyone took would show a 0.

The course missing the most students goes first; on a tie, sort by name.

What it has to return

These rows, in this order

namenot_enrolled
Chess7
Music5
History4
Science3
Math2

This is what comes out with the data in the practice database this challenge uses. The column names don’t count, so name yours however you like: what counts is how many there are and the order they come in. Besides this one, the challenge has hidden tests with other data that are revealed when you submit your solution.

You start with this

SQL

SELECT c.name,
  COUNT(*) AS enrolled
FROM grades g
JOIN courses c
  ON g.course_id = c.id
GROUP BY c.id, c.name;
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n log n) challenges

See all challenges →