SQL查询返回平均值

时间:2016-06-20 14:09:55

标签: sql

我有表grades

id | student_id | grade
-----------------------
1  | 1          | 4
2  | 1          | 5
3  | 2          | 3

我想要一个返回student_id和平均grade的查询:

student_id | avg_grade
----------------------
1          | 4.5
2          | 3

什么查询会这样做?

1 个答案:

答案 0 :(得分:0)

SQL AVG()函数执行此操作:

SELECT student_id, AVG(grade) as avg_grade
FROM grades
GROUP BY student_id

Reference for MySQL's AVG()