SQL Aggregate Functions
Hereβs a clear, complete guide to SQL Aggregate Functions β what they do, how they behave with NULLs, and best-practice examples.
β What Are Aggregate Functions?
Aggregate functions take multiple rows and return a single summarized value.
They are often used with GROUP BY.
β Common SQL Aggregate Functions
1. COUNT()
Counts rows.
-
COUNT(*)counts all rows -
COUNT(column)ignores NULLs
2. SUM()
Adds numeric values.
3. AVG()
Returns the average of numeric values (ignores NULL).
4. MIN() and MAX()
Returns smallest and largest values.
π₯ Using Aggregate Functions with GROUP BY
π HAVING (Filter After Aggregation)
Use HAVING to filter aggregated results (not WHERE).
π§ Important Behavior Notes
β All aggregate functions ignore NULLs, except COUNT(*)
β Aggregates without GROUP BY operate on the entire table
β Cannot reference non-aggregated columns without GROUP BY
π§ Combining Aggregates
You can compute multiple aggregates at once:
