Aggregation
When the overall result matters more than individual rows
So far, we have selected, filtered, and sorted individual rows. But data questions often sound different: how many orders were made, what the average price is, what the total sales were for a day, or how many users belong to each country.
For such tasks, SQL uses aggregation: many rows are reduced to one summary value or to a set of summaries for groups.
What this section contains
- Aggregate functions. We will understand the general idea of functions that calculate one value from a set of rows.
- COUNT. We will cover row counting and the important difference between
COUNT(*)andCOUNT(column). - SUM, AVG, MIN, and MAX. We will learn how to calculate totals, averages, minimums, and maximums.
- GROUP BY. We will move from one summary for the whole result to a separate summary for each group of rows.
- HAVING. We will learn how to filter already formed groups using aggregate results.
- Grouping by multiple columns. We will examine groups defined not by one value, but by a combination of several values.
The topics build naturally: first we calculate a summary for the whole result, then divide rows into groups, and only after that learn how to control those groups.