Not on the roster
- O(n log n) · Hard
- Full plan
- SQL
- subqueries
- joins
- grouping
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
| name | not_enrolled |
|---|---|
| Chess | 7 |
| Music | 5 |
| History | 4 |
| Science | 3 |
| Math | 2 |
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;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
- Often ordered togetherSQL · joins · grouping
- One student, one voteSQL · subqueries · grouping
- Report cards read acrossSQL · joins · grouping · conditionals
- Spent nothing, still on the listSQL · joins · nulls · grouping
- The easy gradersSQL · subqueries · sets · joins
- The hallway listSQL · joins · grouping · sorting