学校的光荣榜只放平均分 80 及以上的学生。按学生分组,算出每人所有科目 score 的平均分(空的分数不算),只留下平均分 80 及以上的人。要 name 和平均分 ROUND(…, 1) 两列。
| id | name | class | age | city |
|---|---|---|---|---|
| 1 | Amy | 1A | 12 | Leeds |
| 2 | Ben | 1A | 13 | York |
| 3 | Cara | 1B | 12 | NULL |
| 4 | Dan | 1B | 14 | Leeds |
| 5 | Eva | 1A | 12 | Hull |
| 6 | Finn | 1C | 13 | NULL |
| 7 | Gus | 1C | 12 | York |
| id | student_id | subject | score |
|---|---|---|---|
| 1 | 1 | math | 92 |
| 2 | 1 | art | 78 |
| 3 | 2 | math | 65 |
| 4 | 2 | art | 88 |
| 5 | 3 | math | 74 |
| 6 | 4 | math | 55 |
| 7 | 4 | art | 90 |
| 8 | 5 | math | 81 |
| 9 | 5 | art | NULL |
| 10 | 7 | math | 99 |
| id | name | class | age | city |
|---|---|---|---|---|
| 1 | Amy | 1A | 12 | Leeds |
| 2 | Ben | 1A | 13 | York |
| 3 | Cara | 1B | 12 | NULL |
| 4 | Dan | 1B | 14 | Leeds |
| 5 | Eva | 1A | 12 | Hull |
| 6 | Finn | 1C | 13 | NULL |
| 7 | Gus | 1C | 12 | York |
| id | student_id | subject | score |
|---|---|---|---|
| 1 | 1 | math | 92 |
| 2 | 1 | art | 78 |
| 3 | 2 | math | 65 |
| 4 | 2 | art | 88 |
| 5 | 3 | math | 74 |
| 6 | 4 | math | 55 |
| 7 | 4 | art | 90 |
| 8 | 5 | math | 81 |
| 9 | 5 | art | NULL |
| 10 | 7 | math | 99 |
SELECT students.name, ROUND(AVG(scores.score), 1) FROM students JOIN scores ON scores.student_id = students.id -- 在这里写:按 students.name 分组,再用 HAVING 留下 AVG(scores.score) >= 80 的 ;
写好了先点「运行」看看结果,没问题再点「提交」。