AVG
suggest changeThe aggregate function AVG() returns the average of a given expression, usually numeric values in a column. Assume we have a table containing the yearly calculation of population in cities across the world. The records for New York City look similar to the ones below:
EXAMPLE TABLE
city_name | population| year |
—— | —— | –– |
New York City | 8,550,405 | 2015 |
New York City | … | … |
New York City | 8,000,906 | 2005 |
To select the average population of New York City from a table containing city names, population measurements, and measurement years for the last ten years:
QUERY
select city_name, AVG(population) avg_population
from city_population
where city_name = 'New York City'
group by city_name;
Notice how measurement year is absent from the query since population is being averaged over time.
RESULTS
city_name| avg_population|
—— | —— |
New York City| 8,250,754|
Note: Whether
AVG()accepts non-numeric values such as dates is dialect-specific. It is not portable SQL.