Averages that mean something
- O(n) · Medium
- Full plan
- SQL
- grouping
- joins
Problem
The principal doesn't trust an average built on two grades, and wants the average of every course that already has at least three grades entered. Grades go from 0 to 10.
Show the course's name, how many grades it has, and its average, rounded to 2 decimals, in that order. Being enrolled with no grade yet isn't having a grade: it doesn't help a course get to three.
What it has to return
These rows, in any order
| name | how_many | average |
|---|---|---|
| Math | 5 | 6.9 |
| History | 3 | 8 |
| Science | 3 | 7 |
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
- Bunched up or spread outSQL · grouping · joins · sorting
- 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