AVG()
Calculates the average (mean) of the values in a numeric column across a group of rows.
Description
AVG is an aggregate function that computes the arithmetic mean of a
numeric column: the sum of all the values divided by how many there are.
It's commonly used for things like average test scores, average price, or
average order size.
Syntax
AVG(column_name)Parameters
| Name | Description | Optional |
|---|---|---|
| expression | The numeric column or expression to average. NULL values are excluded from both the sum and the count used in the calculation. | No |
Return Type
AVG returns a DOUBLE, since an average of integers is often not a whole
number.
Examples
Loading database engine...
Loading database engine...
AVG ignores NULLs, it doesn't treat them as zero
If some rows have a NULL score, AVG(score) divides by the number of
non-NULL scores, not the total row count. If you actually want NULLs
treated as zero, wrap the column in COALESCE(score, 0) first.
Common Mistakes
- Expecting a NULL score to count as zero.
AVGsilently skips NULLs, which can make an average look higher than expected if missing scores should really count as 0. - Not rounding the result.
AVGoften returns a long decimal like86.33333333, which is worth wrapping inROUND()for readability. - Averaging an already-averaged column. Taking
AVGof a column that's itself a per-group average (instead of the raw values) gives a mathematically different, often incorrect, result.
Related Functions
See also: SUM, COUNT, MIN.