The hallway list
- O(n log n) · Hard
- Full plan
- SQL
- joins
- grouping
- sorting
Problem
The school secretary is going to post the list of averages in the hallway, and wants it with names, not student numbers. Grades go from 0 to 10. Each student's average is the average of the grades already entered, rounded to 2 decimals: a pending grade isn't a zero, and it doesn't count.
Show the name and the average, in that order, from the highest average to the lowest; on a tie, by name. Everybody is on the list: someone who has no average yet goes at the end, with the cell empty.
What it has to return
These rows, in this order
| name | average |
|---|---|
| Megan | 8.33 |
| Henry | 7.83 |
| Emma | 7.75 |
| Lucas | 7.75 |
| Julia | 7 |
| Leo | 5.25 |
| Sara | NULL |
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 s.name, g.grade
FROM students s
JOIN grades g
ON g.student_id = s.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.