SQL for BAs: GROUP BY and counts
Count and total rows in groups with GROUP BY. Learn HAVING, nulls, duplicates, monthly counts, funnels, ranking, top-N, running totals and a first look at WITH.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: counting votes. You do not read every ballot aloud. You make one pile per candidate and count each pile. The result is a short list: one line per candidate, with a number.
Daily List is a made-up to-do app (SYNTHETIC / TRAINING ONLY). Its tasks table has one row per task. Sam, the , wants to know how many open tasks each person has.
What it is: (Structured Query Language) asks questions of a database. A table is a grid, a row is one record, and a column is one fact. puts rows into piles and returns one row per pile. An aggregate is a total across many rows, such as a count or a sum. A null is a missing value, not a zero. WHERE drops rows before the piles are made. HAVING drops piles after they are counted. Those two filters are the pair people mix up.
Why it matters: Nina, the , asks who has more than two open tasks. A filter written before the count does not run. A count of dates, rather than of rows, also hides the task with no date.
A group is a summary. Look at the biggest and smallest piles too, because the odd row is hidden inside them.
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 The picture helps a newcomer remember that grouping turns many rows into one summary per pile, and that a summary hides detail.
- Filter rows with
WHERE - Group them
- Count or total
- Filter groups with
HAVING - Check the biggest and smallest
HOW THE PICTURE MAPS
| In the picture | In the work |
|---|---|
| A pile of ballots for one candidate | One group of rows |
| Counting the pile | An aggregate such as a count |
| One line per candidate in the result | One row for each group |
| A blank ballot | A null, a missing value |
| A rule that only piles over a set size are reported | HAVING, which filters groups |
WHERE THIS PICTURE STOPS
- Counts hide the odd ballot that may matter most.
- A pile is counted once. A window function keeps every row and adds a number beside it.
- A blank ballot is clear to the eye, but a null needs its own tests.
HOOK
Pile it, count it, then ask what the pile hides.
REMEMBER IT THIS WAY
A contrast: two neighbours told apart by one test.
trims ballots before they are piled. HAVING trims the piles after they are counted.
| In the trick | What it stands for |
|---|---|
| WHERE | Filter rows before grouping |
| HAVING | A rule that filters groups |
TELL A FRIEND
GROUP BY puts rows into piles and an aggregate counts or totals each pile. WHERE filters rows first, HAVING filters piles afterwards, and nulls are skipped by counts of a column.
OFTEN MIXED UP WITH SQL for BAs: JOIN. That topic combines two tables. This one summarises the combined rows, and ranks them, in groups.
PRACTISE AND KEEP
Sources for this topic — 6 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
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.
- Market practice
PostgreSQL18: date/time functions ↗ — PostgreSQL Global Development Group · PostgreSQL 18: date/time functions; §9.9 typed date arithmetic and truncation/timezones. Publisher page opened6November 2026.
Publisher documentation opened on6November2026. Engine/version scope is explicit; other database implementations may differ. Teaching queries are synthetic, not a required publisher template.
- Market practice
Window Functions (PostgreSQL function reference) ↗ — PostgreSQL Global Development Group · PostgreSQL 18, §9.22 rank, ROW_NUMBER returns bigint, LAG/LEAD; publisher page opened6November 2026. Dialect scope explicit.
PostgreSQL18 publisher documentation opened on6November2026: §9.22 rank, ROW_NUMBER bigint, LAG/LEAD. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.
- Market practice
SELECT: compound select statements (SQLite documentation) ↗ — SQLite project · SQLite rolling SELECT documentation opened6November 2026; §§2–5 joins/grouping/bare columns/compound queries/order/limit. UNION ALL supported; INTERSECT ALL/EXCEPT ALL are not. Synthetic fixtures rechecked on3.53.1; historical3.40.1 observations retain their date/version.
SQLite rolling SELECT documentation opened6November2026; §§2–5 joins/grouping/bare columns/compound queries/order/limit. UNION ALL supported; INTERSECT ALL/EXCEPT ALL are not. Synthetic fixtures rechecked on3.53.1; historical3.40.1 observations retain their date/version.
- 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.