TOPIC 16/2815 MINNOT STARTED

Subqueries and window functions: find the odd row out

Some data checks need a query inside a query, or a running comparison across rows. Learn the two tools that answer questions like latest per user and second highest.

L1–L4 are part of the Pro pass

L0 Explain simply

Analogy: a class ranking. A teacher ranks students by marks inside each class, then picks the top one from each class. The classes are not mixed. To find who is above the class average, the teacher works out the average first, then compares.

Daily List is a to-do list app. Tom, the tester, wants the newest task on each list, and he wants lists that hold more tasks than average.

What it is: A subquery is a query inside a query. A works across related rows, such as numbering them, without collapsing those rows into one. uses that number to keep the newest row in each group. SQL (Structured Query Language) is the language both tools are written in. A table is a grid, a row is one item, and a column is one fact about that item. A window function is easy to mix up with GROUP BY. GROUP BY collapses each group into one row. A window function keeps every row.

Why it matters: The newest task on a list is a row, not a count. If Tom groups by list he loses the title. If he forgets a tie rule, two tasks saved at the same moment can swap places between runs.

(SYNTHETIC / TRAINING ONLY: Daily List and every person named in this lesson are invented.)

PLAIN WORDS · 1 in this section
subquery
a database question placed inside another question

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 Ranking students inside each class.

  1. Group
  2. Rank
  3. Keep
Class ranking

HOW THE PICTURE MAPS

How each part of the picture matches the software
In the pictureIn the software
A classA group
Rank by marksA window function
The class averageA subquery
Top of each classLatest per group

WHERE THIS PICTURE STOPS

  • A class has few students, but groups may be huge.
  • A ranking is fixed, but data changes.
  • A teacher breaks ties by judgement, but a query needs a rule.

HOOK

Group, rank, keep one.

TELL A FRIEND

A class ranking: rank inside each class.

OFTEN MIXED UP WITH Joins and grouping: check data across tables. That topic joins and counts rows. This one handles a query inside a query and ranking within groups.

PRACTISE AND KEEP

Sources for this topic — 2 sources
  1. Market practice

    The SQL patterns behind almost every interview question (techinterview.org) ↗ — techinterview.org · Interview preparation article: lists common SQL patterns, such as latest row per group and gaps and islands, that come up again and again (search excerpt only)

    Interview preparation article. Lists gaps and islands among the common SQL interview patterns. Search excerpt only. · Checked 2026-10-18

    Found through a web search; the page was not opened (search excerpt, page not opened). 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. Implementation-specific

    PostgreSQL documentation ↗ — The PostgreSQL Global Development Group · PostgreSQL 18 official manual, opened 2026-11-06: functions-window §9.22, functions-subquery and queries-table-expressions. 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.