3

How do you get the everage of a mysql table column?

for example:

id | name | age

0 | name1 | 20
1 | name2 | 33
2 | name3 | 18
3 | name4 | 24

I want to get the average of all the ages.

1 Answer 1

17

Mysql supports the avg function, which returns the average value.

SELECT avg(age) as average_age FROM tablename

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much. didn't know it was that easy!
also be aware that if you use AVG (or other aggregate functions) with more than one column you need to use GROUP BY to group records against particular column
Is avg() in MySQL faster than returning the results and calculating it with PHP?
You'd best test this out yourself @JaccoAmersfoort Results will vary on so many levels, maybe your database server is simply faster!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.