Skip to the content

Bunched up or spread out

Problem

Grades go from 0 to 10. A class where everyone is around a 7 averages a 7, and so does a class with a 10 and a 4. The vice principal wants to see which courses have the most spread-out classes: how far apart the highest grade and the lowest grade are.

Show the course's name, its highest grade, its lowest grade, and that spread, in that order. Include only courses with at least two grades entered: with a single grade there's no spread to measure. Sort from the most spread out to the most bunched up and, on a tie, alphabetically.

What it has to return

These rows, in this order

namehighestlowestspread
Math9.545.5
Music107.52.5
Science8.562.5
History972

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, g.grade
FROM grades g
JOIN courses c
  ON g.course_id = c.id
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) challenges

See all challenges →