Skip to the content

Report cards read across

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

namemathhistoryscience
Emma9.5NULL6
Henry5.58NULL
Julia7NULLNULL
Leo4NULL6.5
Lucas8.57NULL
MeganNULL98.5
SaraNULLNULLNULL

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;
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 log n) challenges

See all challenges →