Bunched up or spread out
- O(n) · Medium
- Full plan
- SQL
- grouping
- joins
- sorting
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
| name | highest | lowest | spread |
|---|---|---|---|
| Math | 9.5 | 4 | 5.5 |
| Music | 10 | 7.5 | 2.5 |
| Science | 8.5 | 6 | 2.5 |
| History | 9 | 7 | 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, g.grade
FROM grades g
JOIN courses c
ON g.course_id = c.idIt 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
- Chairs for parents' nightSQL · grouping · joins · sorting
- How many payrolls does it cover?SQL · grouping · joins · decimals
- Not the priciest, not the biggestSQL · grouping · joins
- Pages carried homeSQL · grouping · joins · decimals
- Same book twice doesn't countSQL · grouping · sorting
- The 250 ClubSQL · grouping · joins · decimals