Average score per course
Mediumsqlgroup byhavingaggregate
For every course_id with at least two rows in scores, return course_id and average = the mean score rounded to two decimals (ROUND(..., 2)). Sort by highest average, then by course_id.
Tables
- scores (student_id INTEGER, course_id INTEGER, score INTEGER)
Example 1
scores
| student_id | course_id | score |
|---|---|---|
| 1 | 10 | 90 |
| 2 | 10 | 85 |
| 3 | 20 | 70 |
| 1 | 30 | 60 |
| 2 | 30 | 71 |
| 3 | 30 | 80 |
Expected result
| course_id | average |
|---|---|
| 10 | 87.5 |
| 30 | 70.33 |
Constraints
- 0 ≤ rows ≤ 1000
- 0 ≤ score ≤ 100
- The order of rows matters
Submit checks your solution against 7 tests, including hidden ones.
SQL
-- Write one SELECT query. Tables: scores SELECT