Aggregate Functions (COUNT, SUM, AVG, MIN, MAX)
Collapse many rows into summary metrics with the core aggregate functions.
In plain words
They squeeze a pile of rows into one number: how many, the total, the average, the smallest or the biggest.
Think of it like…
Closing the till at a shop: count the receipts (COUNT), add up the takings (SUM), work out the average sale (AVG), and note the smallest and biggest sale (MIN, MAX).
Where you’ve seen it
SELECT COUNT(*) AS orders, SUM(amount) AS revenue, AVG(amount) AS avg_order, MIN(created_at) AS first_order, MAX(created_at) AS last_order FROM orders WHERE created_at >= '2026-11-01'; gives a month-to-date summary in one row.
Words you’ll hear
- Aggregate
- A function that reduces a set of rows to one value.
- NULL handling
- SUM, AVG, MIN, MAX and COUNT(col) skip NULLs; COUNT(*) counts every row.
- Empty input
- COUNT returns 0; SUM, AVG, MIN and MAX return NULL.
- Average of averages
- Averaging per-group averages, which usually isn't the overall average.
Visual diagram
Theory & example queries
How each one treats NULLs and empty input
SUM, AVG, MIN and MAX look only at non-NULL values, so AVG divides by the number of non-NULL values, not by the number of rows. If missing should mean zero, say so explicitly with AVG(COALESCE(x, 0)).
Over zero rows (or only NULLs), COUNT returns 0 but the others return NULL, which shows up as blank cells or breaks arithmetic in reports. Wrap totals you display: COALESCE(SUM(amount), 0).
| id | customer_id | status | amount | coupon_code |
|---|---|---|---|---|
| 101 | 1 | paid | 249 | NULL |
| 102 | 2 | paid | 120 | WELCOME10 |
| 103 | 1 | shipped | 89.5 | NULL |
SELECT COALESCE(SUM(amount), 0) AS revenueFROM ordersWHERE created_at::date = '2026-12-25';PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Expected output1 sample row
| revenue |
|---|
| 0 |
Note: No orders matched the date, so SUM returned NULL and COALESCE converted it to 0 for the report.
Key takeaway: Only COUNT(*) sees NULLs; only COUNT returns 0 on no rows.
Types, DISTINCT and the average-of-averages trap
Result types differ by database. In SQL Server, AVG over an integer column returns an integer, so the average of 1 and 2 is 1; cast first, as in AVG(CAST(x AS decimal(10,2))). PostgreSQL and MySQL return a decimal. MIN and MAX work on any sortable type: dates, timestamps and text.
Averaging pre-computed averages weights every group equally, however many rows it has. The average order value across all orders is SUM(amount) / COUNT(*) over the orders, not the mean of each day's average. Aggregates also accept DISTINCT (COUNT(DISTINCT user_id)), though SUM(DISTINCT) is rarely what you want.
| day | revenue | orders |
|---|---|---|
| 2026-11-20 | 100 | 1 |
| 2026-11-21 | 600 | 3 |
SELECT SUM(revenue) / SUM(orders) AS avg_order_valueFROM daily_stats;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Expected output1 sample row
| avg_order_value |
|---|
| 175 |
Note: For sample daily totals of revenue 100 / 1 order and revenue 600 / 3 orders, (100 + 600) / (1 + 3) = 175.
rather than AVG(avg_order_value).
Key takeaway: Combine sums and counts, not averages.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Choose the rows
Filter to the rows the metric should cover (this month, paid orders) before aggregating.
WHERE status = 'paid' AND created_at >= '2026-11-01'When to use it & trade-offs
What it is
Aggregate functions turn many rows into one value: COUNT (how many), SUM (total), AVG (mean), MIN and MAX (smallest and largest). Without GROUP BY they summarise the whole result as one row; with GROUP BY they summarise each group. All of them except COUNT(*) ignore NULLs.
Why it matters: Every metric on a dashboard is an aggregate, and small details change the numbers: NULLs skipped by AVG, NULL instead of 0 from SUM on no rows, or integer averages that silently drop decimals. Getting these right is the difference between a report people trust and one they double-check.
When to reach for it
- KPIs: total revenue, order count, average order value
- First and last activity dates with MIN and MAX
- Per-group metrics together with GROUP BY
When to avoid or be careful
- Averaging averages to get an overall figure
- Showing SUM results without COALESCE where empty periods are possible
- Integer AVG in SQL Server without casting
Plain aggregates vs GROUP BY vs window aggregates
Strengths: Simplest overall totals and KPIs.
Watch out: No breakdown by dimension.
Strengths: One row per group with any aggregates.
Watch out: Detail rows are collapsed.
Strengths: Keeps detail rows.
Watch out: Doesn't reduce the number of rows.
Interview signals & numbers to know
When this topic comes up
- “Total / average / count of …”
- “First and last order date per customer”
- Blank cells in a report for days with no data
- “The average looks wrong”
- Weighted average
Benchmarks & rules of thumb
- 0
- COUNT over no rows
- NULL
- SUM / AVG / MIN / MAX over no rows
- 1
- row returned without GROUP BY
Common mistakes & follow-ups
Common interview mistakes
- Assuming AVG counts NULL rows as zero; it skips them.
- Displaying SUM directly, so days with no orders show NULL instead of 0.
- Reporting the average of daily averages as the overall average order value.
Likely interviewer follow-ups
- What do SUM and COUNT return when no rows match?
- How does AVG treat NULL values, and how would you treat them as zero?
- Why is the average of daily averages usually different from the overall average?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Aggregate Functions (COUNT, SUM, AVG, MIN, MAX) with hands-on SQL practice and interview Q&A.