SQL for BAs: subqueries and common table expressions
Answer two-step questions in SQL, such as the second-highest value or duplicate rows, with a query inside a query or with named steps, and know which to pick.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: cooking in labelled bowls. You chop the onions into one bowl and mix the sauce in another. The last step uses the bowls. You do not do the whole meal in one pot.
Daily List is a made-up to-do app (SYNTHETIC / TRAINING ONLY). Nina, the , asks Sam for the second-latest due date, and for the tasks that were saved twice.
What it is: (Structured Query Language) can use one query inside another. A table is a grid, a row is one record, and a column is one fact. A subquery is the inner query. Its result is used by the outer query. A is a named result written at the top with WITH, so later lines can use it like a table. A one-value subquery can stand where a value can. A many-row subquery cannot, and that is where IN or a named step takes over. Those two shapes are the neighbours.
Why it matters: "Second latest" is two steps. The latest date is 2026-10-25. The next one is 2026-10-22. One flat filter cannot say "next" until the first answer exists.
The database may still run the steps in its own order. The bowls are for the reader.
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 work.
THE REAL JOB The picture helps a newcomer remember that a hard question can be answered in stages, each stage kept in a labelled bowl.
- Ask the first question
- Name the result
- Ask the second question
- Check on a small table
- Share the answer
HOW THE PICTURE MAPS
| In the picture | In the work |
|---|---|
| A labelled bowl of chopped onions | A named result in a common table expression |
| Mixing a sauce in a small pot first | A subquery that runs first |
| The last step that uses the bowls | The outer query |
| Taking a bowl for each guest's own plate | A correlated subquery |
| Checking the bowls on the counter before you cook | Checking the stages one at a time |
WHERE THIS PICTURE STOPS
- A cook decides the order of the bowls, but the database may choose its own order.
- Bowls hold food once. A subquery can run again for every row.
- A bowl is visible on the counter. A common table expression (CTE) result may not be saved at all.
HOOK
Name the stage, then use it.
REMEMBER IT THIS WAY
A story beat: one picture-line that carries the order or the cause.
The day Nina asked for the second-latest due date: find the latest date first, then use that answer in the next step.
| In the trick | What it stands for |
|---|---|
| First step | First find the latest date |
| Next step | Then use that answer |
| Named stage | A named result placed at the top of a query with the word WITH |
Not every question needs a stage, so check whether one step already answers it.
TELL A FRIEND
A subquery is a query inside another query, and a gives a step a name at the top. Use names when there are several steps or the result is used twice.
OFTEN MIXED UP WITH SQL for BAs: GROUP BY and counts. That topic counts and totals rows by group. This one uses a result from one step inside the next step.
PRACTISE AND KEEP
Sources for this topic — 4 sources
- Market practice
WITH Queries (Common Table Expressions) (PostgreSQL documentation) ↗ — PostgreSQL Global Development Group · PostgreSQL 18, §7.8 WITH, materialization, recursion and cycle detection; publisher page opened6November 2026. Dialect scope explicit.
PostgreSQL18 publisher documentation opened on6November2026: §7.8 WITH, materialization, recursion and cycle detection. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.
- Market practice
Subquery Expressions (PostgreSQL documentation) ↗ — PostgreSQL Global Development Group · PostgreSQL 18, §9.24 EXISTS/IN/NOT IN NULL semantics; publisher page opened6November 2026. Dialect scope explicit.
PostgreSQL18 publisher documentation opened on6November2026: §9.24 EXISTS/IN/NOT IN NULL semantics. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.
- Market practice
Value expressions and aggregate filters (PostgreSQL documentation) ↗ — PostgreSQL · PostgreSQL 18, §4.2.7–8 aggregate/window expressions; §4.2.14 evaluation limits; publisher page opened6November 2026. Dialect scope explicit.
PostgreSQL18 publisher documentation opened on6November2026: §4.2.7–8 aggregate/window expressions; §4.2.14 evaluation limits. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.
- 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.