AVG function in mysql

By | January 18, 2014

using AVG function will return the average value of particular fields.

Example:

Select * from mydoubts_data

————————————-
|id | jobid | name | salary | age|
————————————-
|1 | 121 | vasanthan| 2000 |32 |
|2 | 122 | John | 3000 |28 |
|3 | 123 | Kiran | 3400 |24 |
|4 | 154 | Raju | 2800 |38 |
|5 | 155 | Raju | 2906 |40 |
|6 | 143 | Kiran | 3400 |42 |

Please see the result of below query

Select AVG(age) from mydoubts_data

—————-
| AVG(age) |
—————
| 34 |
—————

you will get the average of various records using group by clause

select name,AVG(age) from mydoubts_data group by name

————————————-
|id | jobid | name | salary | age|
————————————-
|2 | 122 | John | 3000 |28 |
|3 | 123 | Kiran | 3400 |33 |
|4 | 154 | Raju | 2800 |39 |
|1 | 121 | vasanthan| 2000 |31 |