sqlsever 查询各个学生的平均成绩
时间: 2024-04-17 11:18:55 浏览: 253
假设有以下两个表:
学生表(students):
| id | name |
| ---- | ------ |
| 1 | Alice |
| 2 | Bob |
| 3 | Charlie|
| 4 | David |
成绩表(scores):
| id | student_id | course | score |
| ---- | ----------| ------ | ----- |
| 1 | 1 | Math | 85 |
| 2 | 1 | English| 90 |
| 3 | 2 | Math | 92 |
| 4 | 2 | English| 87 |
| 5 | 3 | Math | 78 |
| 6 | 3 | English| 80 |
| 7 | 4 | Math | 90 |
| 8 | 4 | English| 95 |
则查询各个学生的平均成绩的 SQL 语句如下:
```
SELECT students.name, AVG(scores.score) AS avg_score
FROM students
LEFT JOIN scores ON students.id = scores.student_id
GROUP BY students.name;
```
查询结果如下:
| name | avg_score |
| ------ | ---------|
| Alice | 87.5 |
| Bob | 89.5 |
| Charlie| 79 |
| David | 92.5 |
阅读全文