Aggregation

Turn many rows into a summary value, like a count, total, or average.

What is it?

Sometimes you want a summary, not the rows: "How many orders?" "Total revenue?" "Average order value per customer?" These are aggregation questions: turning many rows into one or a few numbers.

SQL's aggregate functions COUNT, SUM, AVG, MIN, and MAX do this. On their own, they summarize a whole table into one row. With GROUP BY, they summarize per group: one row per distinct value in a column, like "revenue per customer" instead of one grand total.

Explain like I'm 10

If orders are a pile of receipts, aggregation is adding them all up on a calculator. GROUP BY is sorting the receipts into piles by customer first, then totaling each pile, so you get one total per customer.

Examples

A single summary value

SELECT COUNT(*) AS total_orders, SUM(total_cents) AS revenue_cents
FROM orders;

Collapses the whole orders table into one row: how many orders exist, and their combined total.

Summarizing per group

SELECT user_id, COUNT(*) AS order_count, SUM(total_cents) AS revenue_cents
FROM orders
GROUP BY user_id;

Instead of one grand total, this gives one row per user_id, each with that user's order count and revenue.

How it works

Without GROUP BY, an aggregate function folds every row that passes WHERE into one result. With GROUP BY <column>, the database first buckets rows by that column's value, then runs the function in each bucket, giving one row per bucket.

HAVING then filters the group results (for example, "only customers with more than 5 orders"). That's different from WHERE, which filters individual rows before grouping.

Why does it exist?

Individual rows are too detailed for most decisions; nobody reads a million orders to understand revenue. Aggregation lets the database summarize efficiently and correctly, instead of every app pulling raw rows and totaling them in its own code.

When to use it

Use aggregation whenever you need a count, total, average, minimum, or maximum: dashboards, reports, "top N by revenue" features, or any "how many," "how much," or "on average" question.

When not to use it

If you need the rows themselves, use a plain SELECT with WHERE and ORDER BY. Aggregating very large tables on every request without caching can be slow; some apps precompute and store summaries instead.

Common mistakes

  • Selecting a column that's neither aggregated nor grouped alongside an aggregate. Most databases reject it or behave unpredictably.

  • Filtering on an aggregate with WHERE (like WHERE COUNT(*) > 5) instead of HAVING.

  • Forgetting that COUNT(*) counts all rows, including those with empty values, while COUNT(column_name) skips rows where that column is empty.

Practice exercises

  1. Easy:

    Write a query returning the total row count and average price from a products table.

  2. Medium:

    Using GROUP BY, show the number of orders per user_id in an orders table.

  3. Hard:

    Add a HAVING clause to the previous query so it only shows users with more than 3 orders.

Interview questions

What does GROUP BY do?

It buckets rows by one or more columns' values and runs aggregate functions in each bucket, giving one result row per group.

What's the difference between WHERE and HAVING?

WHERE filters individual rows before grouping. HAVING filters the group results after aggregation.

What's the difference between COUNT(*) and COUNT(column_name)?

COUNT(*) counts every row, empty values or not. COUNT(column_name) counts only rows where that column has a value.