SQL aggregate functions summarize data across one or more rows and return a single scalar result. They are most often used with GROUP BY to produce per-group summaries, or without GROUP BY to summarize an entire table. Standard aggregates include COUNT, SUM, AVG, MIN, and MAX. Some dialects add functions like STRING_AGG, MEDIAN, or VAR_SAMP. Aggregates ignore NULL values except COUNT(*), which counts every row. DISTINCT can be applied inside most aggregates to remove duplicates before computation. Aggregate results are calculated after WHERE but before HAVING and ORDER BY, so HAVING is the correct place to filter on aggregated values. Aggregates cannot reference aliases defined in the same SELECT list, and non-aggregated columns in the SELECT clause must appear in GROUP BY, otherwise the query is invalid.
aggregate_function
(string) - One of COUNT, SUM, AVG, MIN, MAX, or another supported aggregate.ALL
(DISTINCT) - keyword|||Optional. ALL (default) includes all rows. DISTINCT removes duplicates before aggregation.expression
(any) - Column name or expression to aggregate.grouping_columns
(list) - Columns that define each group when GROUP BY is used.condition / aggregate_condition
(condition) - Boolean expressions for row and group filtering, respectively.GROUP BY, HAVING, DISTINCT, Window Functions, ORDER BY, SELECT
SQL-86
COUNT(*) counts every row, including those where the column is NULL. COUNT(column) skips rows with NULL in that column.
Use the HAVING clause. WHERE filters individual rows before aggregation, while HAVING filters aggregated groups.
Yes. For example, COUNT(DISTINCT user_id) counts unique users. DISTINCT removes duplicates before the aggregation is calculated.
Window functions provide per-row results over a window frame, whereas aggregate functions collapse rows into a single value per group. They can be combined in the same query but serve different purposes.