Core Aggregate Functions: COUNT, SUM, AVG, MIN, MAX
A single query demonstrating all five core aggregates over the employees table.
Overview
SQL's five core aggregates (COUNT, SUM, AVG, MIN, and MAX) summarize
a column across rows. Run without GROUP BY, they collapse the whole table into
a single summary row. This example reports headcount, total payroll, average
salary, and the pay range across all employees.
The Query
Loading playground environment...
Step-by-Step Breakdown
COUNT(*): number of employee rows.SUM(salary): total payroll.AVG(salary): mean salary (rounded for display).MIN(salary)/MAX(salary): the pay floor and ceiling.
Aggregates without GROUP BY
When no GROUP BY is present, the query returns exactly one row, the
aggregate over the entire (filtered) table.
Variations
Aggregate over just the Engineering department:
Loading playground environment...
Common Mistakes
- Mixing aggregates with bare columns.
SELECT first_name, COUNT(*)without aGROUP BYis an error, either group byfirst_nameor drop it. - Expecting NULLs to count.
COUNT(salary)skipsNULLsalaries; useCOUNT(*)to count rows regardless. MIN/MAXon text. These work on strings too (lexicographic), which can surprise you if you expected numeric behavior.
Cite this resource
SQLSimplified. "Core Aggregate Functions: COUNT, SUM, AVG, MIN, MAX Example". Available at: https://sqlsimplified.online/examples/aggregate-functions