-5

I have table grades:

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

I want a query that returns student_id and average grade:

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

What query would do that?

Kaarel Purde
  • 1,255
  • 4
  • 18
  • 38
  • 4
    Did you do any research at all? – Ivar Jun 20 '16 at 14:11
  • 1
    Possible duplicate of [Average of grouped rows in Sql Server](http://stackoverflow.com/questions/3100921/average-of-grouped-rows-in-sql-server) – Ivar Jun 20 '16 at 14:14
  • Possible duplicate of [SQL: Finding Average Score](http://stackoverflow.com/questions/17098020/sql-finding-average-score) – mirosval Jun 20 '16 at 14:21

1 Answers1

0

SQL's AVG() function does that:

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

Reference for MySQL's AVG()

TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94