What is data aggregation?
Definition
Aggregation combines records into summary values, such as a count, total or average. A query can summarise the whole selected dataset or calculate a separate result for each group, such as country. The result depends on both the calculation and which records belong in each group.
In SQL, GROUP BY creates a separate summary for each category. Before calculating a total, check whether joins or repeated records have changed how many times each business event appears.
Why it matters
An overall total can make a large dataset easier to understand, but it can also hide the differences that matter. An average order value may change because individual orders changed, because the mix of customers changed or because the query includes a different set of records. The summary alone does not establish the explanation.
Choose the calculation and grouping together. Check whether you need a count of rows or distinct customers, whether an average should be weighted and how missing values are treated. Be careful when combining summaries: adding counts from overlapping groups can count the same person more than once. Compare the aggregate with a few underlying records so the summary remains connected to the data it represents.
Example: totals and averages by country
These three completed orders are all in euros. Each row represents one order.
| order_id | country | order_total |
|---|---|---|
| 101 | NL | 100.00 |
| 102 | NL | 50.00 |
| 103 | DE | 80.00 |
This SQL counts the orders and calculates their total and average value for each country:
SELECT country,
COUNT(*) AS order_count,
SUM(order_total) AS total_value,
ROUND(AVG(order_total), 2) AS average_value
FROM orders
GROUP BY country
ORDER BY country;
| country | order_count | total_value | average_value |
|---|---|---|---|
| DE | 1 | 80.00 | 80.00 |
| NL | 2 | 150.00 | 75.00 |
The country is the dimension; count, total and average are different summaries of the same orders. The overall average is €230 ÷ 3 = €76.67. Averaging the two country averages would give €77.50, because it would give the one-order and two-order groups equal weight.