What is an aggregate function in SQL?
An aggregate function takes many rows and returns a single summary value. The common ones are COUNT, SUM, AVG, MIN, and MAX. They are often used with GROUP BY to produce a summary per group, such as the total sales for each region.
- Aggregate functions compute a single value from a set of rows.
- Common ones are COUNT, SUM, AVG, MIN, and MAX.
- They are often paired with GROUP BY to summarise each group.
The common aggregates
- COUNT: how many rows.
- SUM: the total of a numeric column.
- AVG: the average value.
- MIN and MAX: the smallest and largest values.
SELECT region, SUM(amount) AS total
FROM sales
GROUP BY region;
-- one total per region
Without GROUP BY an aggregate summarises the whole table into one row. With GROUP BY it summarises each group separately.
Watch the COUNT trap. COUNT(*) counts all rows, while COUNT(column) skips nulls in that column. Mentioning this difference shows you know how nulls quietly change results.
Frequently asked questions
Does COUNT include null values?
COUNT(*) counts all rows including nulls, while COUNT(column) counts only rows where that column is not null.
Can you filter on an aggregate result?
Yes, using HAVING after GROUP BY. For example, HAVING COUNT(*) greater than 5 keeps only groups with more than five rows.
Common follow up questions
Related interview questions
Want the full SQL guide?
Read every SQL concept with notes, diagrams, and code in one place. Track your progress as you go.
Open the SQL guide All SQL questions