TOPIC 17/2816 MINNOT STARTED

case: labels and buckets in a query, and how to test them

Learn how case turns values into labels or buckets, what it returns when nothing matches, and how to test the edges of each rule so a label on a screen is never silently wrong.

L1–L4 are part of the Pro pass

L0 Explain simply

Analogy: a sorting belt at a post office. A parcel goes down the belt. The first gate that fits pushes it into a bin: small, medium or large. If no gate fits, the parcel falls off the end.

Daily List is a to-do list app. A task is labelled overdue, today or later from its due date. Tom tests the day on each side of the cut, and the task with no date. (SYNTHETIC / TRAINING ONLY: Daily List and the people here are invented.).

What it is: A is the (Structured Query Language) form CASE. SQL asks questions of a database and can change it. A table is a grid. A row is one task. A column is one fact, such as due_date. CASE walks its tests in order and returns the value for the first test that is true. The neighbour is a chain of WHERE queries. WHERE keeps or drops rows. CASE keeps every row and adds a label.

Why it matters: A can leave the label blank. Or the says later, which sends Lily to do a task that has no day. The screen looks sure either way.

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 post-office belt with gates that sort parcels into bins.

  1. Edges
  2. Order
  3. Floor
Chute

HOW THE PICTURE MAPS

How each part of the picture matches the software
In the pictureIn the software
A gateA WHEN condition
A binA returned label
The floor under the beltA missing else
The first gate that fitsThe first true condition

WHERE THIS PICTURE STOPS

  • A belt has a few gates, but a rule can have many conditions.
  • A parcel is seen to fall, but a null is silent.
  • Gates are fixed, but a query is read by the database.

HOOK

First gate that fits wins.

TELL A FRIEND

A belt: the first gate that fits wins.

OFTEN MIXED UP WITH SQL (Structured Query Language) filtering, sorting and totals for testers. That topic covers filtering and totals. This one adds labels and buckets made by case.

PRACTISE AND KEEP

Sources for this topic — 3 sources
  1. Market practice

    Conditional expressions (PostgreSQL documentation) ↗ — PostgreSQL · PostgreSQL reference: a CASE with no ELSE and no matching condition returns null (search excerpt, page not opened)

    §9.18 CASE/COALESCE/NULLIF. PostgreSQL18 implementation, not a universal SQL mandate. · Checked 2026-11-06

    PostgreSQL18 publisher documentation opened on6November2026: §9.18 CASE/COALESCE/NULLIF. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.

  2. Market practice

    SELECT command reference (PostgreSQL documentation) ↗ — PostgreSQL · PostgreSQL select reference: select output names can be given with as, and rows are filtered by where before grouping (search excerpt, page not opened)

    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.

  3. Implementation-specific

    PostgreSQL documentation ↗ — The PostgreSQL Global Development Group · PostgreSQL 18 official manual, opened 2026-11-06: functions-conditional §9.18 and functions-datetime §9.9.5. 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.