Aggregate Functions (Transact-SQL)

THIS TOPIC APPLIES TO: yesSQL Server (starting with 2008)yesAzure SQL DatabaseyesAzure SQL Data Warehouse yesParallel Data Warehouse

An aggregate function performs a calculation on a set of values, and returns a single value. Except for COUNT, aggregate functions ignore null values. Aggregate functions are often used with the GROUP BY clause of the SELECT statement.

All aggregate functions are deterministic. In other words, aggregate functions return the same value each time that they are called, when called with a specific set of input values. See Deterministic and Nondeterministic Functions for more information about function determinism. The OVER clause may follow all aggregate functions, except the GROUPING or GROUPING_ID functions.

Use aggregate functions as expressions only in the following situations:

  • The select list of a SELECT statement (either a subquery or an outer query).
  • A HAVING clause.

Transact-SQL provides the following aggregate functions:

AVG MIN
CHECKSUM_AGG SUM
COUNT STDEV
COUNT_BIG STDEVP
GROUPING STRING_AGG
GROUPING_ID VAR
MAX VARP

See also

Built-in Functions (Transact-SQL)
OVER Clause (Transact-SQL)