Report cards read across
- O(n log n) · Hard
- Full plan
- SQL
- joins
- grouping
- conditionals
Problem
In grades, every grade sits in its own row. The printed report card doesn't work that way: it has one row per student and one cell per course. This sheet covers the three classroom subjects, Math, History, and Science, which are courses 1, 2, and 3. Music and Chess go on another sheet.
Show each student's name and their grades in Math, History, and Science, in that order, sorted alphabetically by name.
Everyone is on the sheet, including anyone who isn't taking any of the three courses. The cell for a course a student isn't taking stays empty, and so does the cell for a grade that hasn't been entered: on paper you can't tell them apart.
What it has to return
These rows, in this order
| name | math | history | science |
|---|---|---|---|
| Emma | 9.5 | NULL | 6 |
| Henry | 5.5 | 8 | NULL |
| Julia | 7 | NULL | NULL |
| Leo | 4 | NULL | 6.5 |
| Lucas | 8.5 | 7 | NULL |
| Megan | NULL | 9 | 8.5 |
| Sara | NULL | NULL | 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.course_id,
g.grade
FROM students s
JOIN grades g
ON g.student_id = s.id
ORDER BY s.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
- Spent nothing, still on the listSQL · joins · nulls · grouping
- The easy gradersSQL · subqueries · sets · joins
- The hallway listSQL · joins · grouping · sorting
- Their latest, front and centerSQL · subqueries · joins
- Trailing their own teamSQL · subqueries · nulls
- Two at a timeSQL · joins · nulls · comparisons