Skip to content
SQLSimplified
aggregate

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

  1. COUNT(*): number of employee rows.
  2. SUM(salary): total payroll.
  3. AVG(salary): mean salary (rounded for display).
  4. 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 a GROUP BY is an error, either group by first_name or drop it.
  • Expecting NULLs to count. COUNT(salary) skips NULL salaries; use COUNT(*) to count rows regardless.
  • MIN/MAX on 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