aggregate

AVG

Computes the arithmetic mean of non-NULL numeric values. NULL rows are excluded from both the sum and the divisor, so AVG is not equivalent to SUM/COUNT(*) when NULLs are present.

AVG([DISTINCT] numeric_expression)

Parameters / Modifiers

Parameter Purpose
DISTINCT Average of unique values only
OVER (...) Moving averages when used as a window function
FILTER (WHERE ...) Postgres: only include rows matching a predicate

Examples

SELECT AVG(price) FROM products;

Mean price across all non-NULL prices

SELECT ROUND(AVG(rating), 2) FROM reviews;

Mean rating rounded to two decimal places

SELECT category, AVG(price) FROM products GROUP BY category;

Average price per category

SELECT AVG(score) OVER (PARTITION BY class_id) FROM tests;

Class average attached to each row

Dialect notes / Gotcha

In Postgres, AVG on an integer column returns numeric; in MySQL it returns a DECIMAL / DOUBLE. Empty groups return NULL — coalesce if a numeric default is required.

Related functions

← All SQL functions · SQL cheat sheet · Window functions · PostgreSQL cheat sheet