Name

AVG and SUM

Synopsis

The AVG function computes the average of values in a column or an expression. SUM computes the sum. Both functions work with numeric values and ignore NULL values. They also can be used to compute the average or sum of all distinct values of a column or expression.

AVG and SUM are supported by Microsoft SQL Server, MySQL, Oracle, and PostgreSQL.

Example

The following query computes average year-to-date sales for each type of book:

SELECT   type, AVG( ytd_sales ) AS "average_ytd_sales"
FROM     titles 
GROUP BY type;

This query returns the sum of year-to-date sales for each type of book:

SELECT   type, SUM( ytd_sales ) 
FROM     titles 
GROUP BY type;

Get SQL in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.