Aggregation in depth: HAVING, distinct counts and grouping levels
Counting is more than counting every row. Learn where a filter goes before or after grouping, how distinct counts differ, and how to add subtotal rows.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: a ballot count. First you throw out the spoiled papers. Then you tally by ward. Then you announce only the wards with enough votes. Throwing out a paper and dropping a ward are two different steps.
Daily List is a made-up to-do app (SYNTHETIC / TRAINING ONLY). Sam, the , counts open tasks per list. Nina, the , wants only the lists that have more than one open task.
What it is: (Structured Query Language) is a language for asking a database questions. A table is a grid, a row is one task, and a column is one fact, such as the status. A filters groups after they are counted. A distinct count counts different values once. A grouping level is one layer of total, such as the detail, the subtotal, or the grand total. You write them in the query. The neighbour of HAVING is WHERE, which filters rows before the count.
Why it matters: A count that still includes done tasks makes both lists look busy. Sam would flag list 8, which has only one open task.
A person can see four papers and tally them. A query hides which step threw a row out.
PLAIN WORDS · 1 in this section
- subtotal
- a total of part of a list
PICTURE IT
The picture is the one the lesson above opens with. The table below shows how each part of it matches the work.
THE REAL JOB A ballot count with spoiled papers, wards and big wards.
- Filter
- Group
- Keep
HOW THE PICTURE MAPS
| In the picture | In the work |
|---|---|
| Throwing out spoiled papers | WHERE |
| Tallying by ward | GROUP BY |
| Announcing big wards only | HAVING |
| Counting different voters | COUNT DISTINCT |
WHERE THIS PICTURE STOPS
- A ballot is paper, but rows are data.
- Papers are thrown out by hand, but WHERE runs in the database.
- Wards are few, but groups can be thousands.
HOOK
Throw out, tally, announce.
TELL A FRIEND
A ballot count: spoiled papers out, then wards, then the big ones.
OFTEN MIXED UP WITH SQL for BAs: GROUP BY and counts. That topic introduces group by. This one adds HAVING, distinct counts and subtotals.
PRACTISE AND KEEP
Sources for this topic — 4 sources
- Market practice
SELECT command reference (PostgreSQL documentation) ↗ — PostgreSQL · PostgreSQL 18, SELECT: WHERE/HAVING/output aliases, functional dependence, NULL ordering and LIMIT; publisher page opened6November 2026. Dialect scope explicit.
PostgreSQL18 publisher documentation opened on6November2026: SELECT: WHERE/HAVING/output aliases, functional dependence, NULL ordering and LIMIT. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.
- Market practice
Table expressions and joined tables (PostgreSQL documentation) ↗ — PostgreSQL · PostgreSQL 18, §7.2 joined tables, grouping and grouping sets; publisher page opened6November 2026. Dialect scope explicit.
PostgreSQL18 publisher documentation opened on6November2026: §7.2 joined tables, grouping and grouping sets. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.
- Market practice
Aggregate functions (PostgreSQL documentation) ↗ — PostgreSQL · PostgreSQL 18, §9.21 aggregates, NULL and empty input; publisher page opened6November 2026. Dialect scope explicit.
PostgreSQL18 publisher documentation opened on6November2026: §9.21 aggregates, NULL and empty input. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.
- Simplified educational illustration
Payments Signal editorial teaching models — Payments Signal
What this simplifies: Daily List people, data and exercises are invented for teaching. Query results belong to the declared fixture and database version. Authorization, business identity, time bounds and unknown-value policy must be agreed in context; no universal mandatory query template is implied.
Used wherever diagrams, scenarios, figures, or example values are didactic constructions rather than sourced facts; every such use carries a simplifications disclosure. All people, companies, banks, and list entries in examples are fictional.
Deepest material on this page: L4 — Standards & sources. Where a topic stops short of implementation depth, that is a deliberate coverage decision, not an oversight — see coverage.