TOPIC 09/3018 MINNOT STARTED

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.

  1. Filter rows with WHERE
  2. Group them
  3. Count or total
  4. Filter groups with HAVING
  5. Check the biggest and smallest
From rows to a summary

HOW THE PICTURE MAPS

How each part of the picture matches the work
In the pictureIn the work
A pile of ballots for one candidateOne group of rows
Counting the pileAn aggregate such as a count
One line per candidate in the resultOne row for each group
A blank ballotA null, a missing value
A rule that only piles over a set size are reportedHAVING, 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.

What each part of the trick stands for
In the trickWhat it stands for
WHEREFilter rows before grouping
HAVINGA 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
  1. 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.

    SELECT: WHERE/HAVING/output aliases, functional dependence, NULL ordering and LIMIT. PostgreSQL18 implementation, not a universal SQL mandate. · Checked 2026-11-06

    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.

  2. Market practice

    Aggregate functions (PostgreSQL documentation) ↗ — PostgreSQL · PostgreSQL 18, §9.21 aggregates, NULL and empty input; publisher page opened6November 2026. Dialect scope explicit.

    §9.21 aggregates, NULL and empty input. PostgreSQL18 implementation, not a universal SQL mandate. · Checked 2026-11-06

    PostgreSQL18 publisher documentation opened on6November2026: §9.21 aggregates, NULL and empty input. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.

  3. 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.

    §9.9 typed date arithmetic and truncation/timezones. · Checked 2026-11-06

    Publisher documentation opened on6November2026. Engine/version scope is explicit; other database implementations may differ. Teaching queries are synthetic, not a required publisher template.

  4. 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.

    §9.22 rank, ROW_NUMBER bigint, LAG/LEAD. PostgreSQL18 implementation, not a universal SQL mandate. · Checked 2026-11-06

    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.

  5. 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.

    Says EXCEPT returns the rows from the left query that the right query does not produce, and that both sides need the same number of columns. · Checked 2026-11-06

    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.

  6. Simplified educational illustration

    Payments Signal editorial teaching models — Payments Signal

    This site's own simplified teaching models. · Checked 2026-07-12

    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.