Calculate average of each class of students

Viewed 87

I got 2 tables like that:

STUDENT

ID | Name   | Class
1  | Jonh   | 12   
2  | Smit   | 11   
3  | David  | 10   
4  | Simon  | 11   
5  | Kate   | 12   
6  | Marry  | 11

SCORE

Studentid | Score  
4         |  10   
1         |  5
2         |  7
3         |  9
5         |  8

I need to calculate Average for each class like this

Class| Average
10    | 9 
11    | 8.5
12    | 6.5

Please help me to do that. I tried this :

SELECT stud.class, AVG(scor.mark) AS avg_mark
  FROM STUDENT stud
  INNER JOIN SCORE scor ON stud.id = scor.studentid
  GROUP BY stud.class

Any help would be greatly appreciated

1 Answers

Your query looks correct, but there is one error in your query

round(avg(scor.score), 1) is to round avg values

try this

SELECT stud.class,  round(avg(scor.score), 1) AS avg_mark
  FROM STUDENT stud
  INNER JOIN SCORE scor ON stud.id = scor.studentid
  GROUP BY stud.class

Column mark is not exists in Score table.

Related