AVG
Returns the arithmetic mean of the expression values in a group of rows.
MySQL 8.1
AVG([DISTINCT] expr)Parameters
- DISTINCTkeywordoptional
- Use each value only once
- exprDECIMAL
- Numeric expression to average
Return value
A number: DECIMAL for integer and DECIMAL values, DOUBLE for floating-point numbers; NULLs are ignored, NULL for an empty set
Examples
Average price in payments
MySQL 8.1
SELECT AVG(unit_price) AS avg_price
FROM Payments;Average price per family member
MySQL 8.1
SELECT family_member,
AVG(unit_price) AS avg_price
FROM Payments
GROUP BY family_member;Details
NULL values take part in neither the sum nor the count, so with missing values AVG(x) is not equal to SUM(x) / COUNT(*). To treat missing values as zeros, use AVG(IFNULL(x, 0)). The average of integers is shown with four decimal places: 3231.1786.