Skip to content
Advertisement

how to find the average of first 3 rows using avg() function in MySQL

I have table called student, and which there is field of marks. i’m trying to get an average of first 3 marks.

    +-----------+
    |   MARKS   |
    +-----------+
    |   34.8771 |
    |   34.8282 |
    |   35.9533 |
    |      36.1 |
    |      36.3 |
    |     37.15 |
    +-----------+

I tried by following query,

select avg(marks) from student limit 3;

and i got following output,

+-------------------+
|    avg(marks)     |
+-------------------+
| 22.35627736719113 |
+-------------------+

i’m expecting the output as 35.21953333333333

explanation, (34.8771+34.8282+35.9533)/3 = 35.21953333333333

Please help me with this.

Advertisement

Answer

Select avg(marks) from
(
Select marks from students limit 3
) as alias
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement