Chairs for parents' night
- O(n) · Medium
- Full plan
- SQL
- grouping
- joins
- sorting
Problem
It's parents' night, and each teacher hosts the families of their own students. A teacher sets out one chair per student and hands over one progress report for each course that student takes with them: the family of a student who takes both of a teacher's courses gets one chair and two reports.
For each teacher with students enrolled, show the teacher's name, how many chairs to set out, and how many reports to hand over, in that order. Being enrolled is enough: when a grade is still pending, the report goes out with the grade left blank.
Whoever sets out the most chairs goes first; ties go alphabetically.
What it has to return
These rows, in this order
| teacher | chairs | reports |
|---|---|---|
| Rose | 6 | 9 |
| Paul | 3 | 3 |
| Irene | 2 | 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.teacher, g.student_id
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
- 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
- The notebook by the registerSQL · grouping · joins