TOPIC 10/2816 MINNOT STARTED

Aggregation in depth: HAVING, distinct counts and subtotals

A count on a screen can mean rows, values or people. Learn where a filter goes before or after grouping, how a distinct count differs, and how to check subtotals.

L1–L4 are part of the Pro pass

L0 Explain simply

Analogy: a bakery count. The baker first throws out the burnt rolls. Then she counts the rolls on each shelf. Then she reports only the shelves that hold more than three. A test on a roll is not a test on a shelf. The order is the whole story.

Daily List is a to-do list app. A screen lists the lists that have more than three open tasks. Tom checks which filter ran before the count, and which filter ran after it. (SYNTHETIC / TRAINING ONLY: Daily List and the people here are invented.).

What it is: A is the (Structured Query Language) clause HAVING. SQL asks questions of a database and can change it. A table is a grid of tasks. A row is one task. A column is one fact, such as status or list_id. WHERE drops rows before they are grouped. HAVING drops groups after the count. People mix those two up, and they also mix COUNT(*) with COUNT(DISTINCT user_id). One counts tasks. The other counts people.

Why it matters: A list with exactly three open tasks can show when it should not. A stops a count of tasks being labelled as people. A can also fail to add up. The figure looks precise and is the wrong kind of thing.

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

THE REAL JOB A bakery count with burnt rolls and busy shelves.

  1. Ask
  2. Run
  3. Compare
Bakery count

HOW THE PICTURE MAPS

How each part of the picture matches the software
In the pictureIn the software
Removing burnt rollsWHERE
Counting trays by shelfGROUP BY
Reporting busy shelves onlyHAVING
Counting different recipesCOUNT DISTINCT

WHERE THIS PICTURE STOPS

  • Loaves are real, but rows are data.
  • A baker removes rolls by hand, but WHERE runs in the database.
  • Shelves are few, but groups can be thousands.

HOOK

Throw out, tally, announce.

TELL A FRIEND

A bakery: burnt rolls out first, busy shelves last.

OFTEN MIXED UP WITH Joins and grouping: check data across tables. That topic joins and groups rows. This one adds , distinct counts and subtotals.

PRACTISE AND KEEP

Sources for this topic — 3 sources
  1. Market practice

    SELECT command reference (PostgreSQL documentation) ↗ — PostgreSQL · PostgreSQL select reference: where filters individual rows before grouping, having filters the groups that group by makes, and aggregates are worked out before the having clause is read (search excerpt, page not opened)

    The PostgreSQL reference for the select command, including the where, group by and having clauses. Official project documentation. · Checked 2026-10-31

    Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.

  2. Market practice

    Table expressions and joined tables (PostgreSQL documentation) ↗ — PostgreSQL · PostgreSQL table expressions page: grouping sets group the rows separately for each set, and rollup is shorthand for a list of grouping sets that adds subtotal rows (search excerpt, page not opened)

    The PostgreSQL reference for joined tables: inner, left, right, full and cross joins, and how an ON condition differs from a WHERE condition in an outer join. Official project documentation. · Checked 2026-10-31

    Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.

  3. Implementation-specific

    PostgreSQL documentation ↗ — The PostgreSQL Global Development Group · PostgreSQL 18 official manual, opened 2026-11-06: queries-table-expressions §7.2 and functions-aggregate. Supports only PostgreSQL behavior; earlier execution evidence remains separately versioned.

    A free, precise, section-numbered description of the SQL a payments practitioner actually writes. Join types and what happens to unmatched rows, GROUP BY and HAVING against WHERE, window functions and the de-duplication and running-total idioms built on them. Transaction isolation levels and the anomalies each permits, index use, and null three-valued logic.

    Used here for the data-and-SQL questions a Payment Operations Analyst, a Production Support Analyst and a Sanctions Screening Analyst are asked. · Checked 2026-08-18

    An implementation, not the standard — which is the point. ISO/IEC 9075-2 is paywalled, and this documents the same constructs in text anybody can open. Its isolation-level chapter attributes to the standard explicitly, so it can carry a standard-level claim as long as the citation says which document it is quoting.

    Cited as market practice rather than an official standard by kind: an engine's manual is authoritative about that engine and is not the obligation. Where a question turns on behaviour PostgreSQL chose rather than behaviour SQL requires, the answer has to say so.

    One correction carried here because it circulates widely: the sentence "The fundamental difference between WHERE and HAVING…", quoted all over the internet as PostgreSQL documentation, is not in the current documentation. It was searched for in the source tree and is not there.

    Read through a mirror, because the publisher's own domain could not be fetched (a reachability gap, not a statement about the publisher). A 403 there is a policy refusal and says nothing about whether the document exists or is reachable elsewhere.

    Anyone on an unrestricted network should open the canonical URL above and confirm the section and page numbers cited against it. Every claim drawn from this entry is written so that it can be checked against a numbered page.

    Read from the postgres/postgres documentation source tree (doc/src/sgml), which the project itself maintains and which carries the section ids the published URLs are derived from — so a cited section id can be checked against the rendered page.

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.