AAPL has 6,699 daily bars in this table, but a risk report wants one number: its average close, or its volume per year. Aggregation turns thousands of rows into a handful, and the one question that trips people up is whether to filter before or after.
The idea in plain words
Where we are. The previous topic selected and filtered individual rows. Here the output has fewer rows than the input: each output row summarises a group. The next topic deals with missing values, which aggregates treat in a special way.
An aggregate function reads many rows and returns one value: count, sum, avg, min, max. Alone, it summarises the whole table. With GROUP BY the rows are first sorted into groups by the value of an expression, and the aggregate runs once per group.
All the rows that share the same value of the GROUP BY expression. Each group becomes exactly one output row.
One row for the whole table
# Aggregates over every AAPL bar: one output rowprint(con.sql(""" SELECT count(*) AS days, min(low) AS lowest, max(high) AS highest, round(avg(close), 2) AS mean_close1 FROM bars WHERE symbol = 'AAPL'"""))
┌───────┬──────────┬──────────┬────────────┐ │ days │ lowest │ highest │ mean_close │ │ int64 │ double │ double │ double │ ├───────┼──────────┼──────────┼────────────┤ │ 6699 │ 0.190585 │ 344.5699 │ 53.96 │ └───────┴──────────┴──────────┴────────────┘
- 1Four aggregates, one row.
count(*)counts rows;avg(close)ignores any NULL close.
One row per group
List the grouping expression in GROUP BY and again in SELECT. Every column in SELECT must be either one of the grouped expressions or inside an aggregate: asking for date next to GROUP BY year(date) makes no sense, because a year has many dates.
# Total volume of KO per calendar year, last three yearsprint(con.sql(""" SELECT year(date) AS year, sum(volume) AS total FROM bars WHERE symbol = 'KO' GROUP BY year(date)1 ORDER BY year DESC LIMIT 3"""))
┌───────┬────────────┐ │ year │ total │ │ int64 │ int128 │ ├───────┼────────────┤ │ 2026 │ 2746116852 │ │ 2025 │ 4169819546 │ │ 2024 │ 3521445384 │ └───────┴────────────┘
- 1One output row per year.
WHERE before, HAVING after
WHERE removes rows before any grouping happens, so it can only talk about single rows. HAVING removes whole groups after the aggregates are known, so it is the only place to say "keep symbols whose total volume is at least ...". Filter early with WHERE whenever you can; it means less work for the database.
# WHERE keeps 2026 bars; HAVING then keeps symbols with a big enough totalprint(con.sql(""" SELECT symbol, sum(volume) AS total FROM bars WHERE date >= '2026-01-01'1 GROUP BY symbol HAVING sum(volume) >= 50000000002 ORDER BY total DESC"""))
┌─────────┬────────────┐ │ symbol │ total │ │ varchar │ int128 │ ├─────────┼────────────┤ │ AAPL │ 8114576355 │ └─────────┴────────────┘
- 1Row filter, before grouping.
- 2Group filter, after the sums exist.
An aggregate with no GROUP BY always returns exactly one row, even over zero input rows: count(*) is 0 and min, max, avg are NULL. With GROUP BY an empty input has no groups, so it returns no rows. The grader checks both cases.
What the tasks ask
- 1`summary`Four aggregates for one symbol in a single row.
- 2`yearly_volume`
GROUP BY year(date)and a sum, oldest year first. - 3`symbol_spans` and `heavy_symbols`The first filters rows with
WHEREbefore grouping; the second filters groups withHAVINGafter.
Lab · what's graded
4 tasks · 100 pts- One summary row:
summary25 pts GROUP BYyear:yearly_volume25 ptsWHEREbefore the groups:symbol_spans25 ptsHAVINGafter the groups:heavy_symbols25 pts