AVG is a standard SQL aggregate and analytic function. As an aggregate, it scans all qualifying rows (or each GROUP BY group) and calculates the arithmetic mean of the supplied numeric expression. Nulls are ignored, while non-numeric data types must be implicitly convertible to a numeric type or the query fails. If no non-NULL rows exist, the result is NULL.AVG can also operate as a window function when used with an OVER clause. In this mode, it returns the average for the current row’s window frame while leaving other rows visible, enabling running averages, moving averages, and other advanced analytics.The result data type depends on the input and the dialect. Most engines promote integer inputs to decimal or floating types to preserve precision. DISTINCT may be applied to average only unique values. Performance can degrade on very large datasets without proper indexing or partitioning.
numeric_expression
- Numeric or implicitly numeric column or expression to average.DISTINCT
(ALL) - Optional keyword to average only distinct values (default ALL).OVER()
- Optional analytic clause defining partitioning, ordering, and frame for window averages.SQL-86 (ANSI SQL)
No. AVG skips NULL values. If every value evaluated is NULL, the function returns NULL, not zero.
Use the window form: `AVG(column) OVER (ORDER BY date_column ROWS BETWEEN n PRECEDING AND CURRENT ROW)` to compute a moving window average.
Use `AVG(DISTINCT column)` when duplicates would distort the metric, such as averaging unique product prices instead of every recorded sale.
Most engines upcast integers to decimal or floating types to retain precision. Cast the result back to an integer if truncation is acceptable.