Manuals / SQL / Chapter 7

B · Analyze · intermediate · Week 2 · Chapter 7 of 12

Subqueries & CTEs

Subqueries in WHERE/FROM. WITH clause (CTE) for readable multi-step queries. Prefer CTEs over nested subqueries when clarity matters.

Path progress
53%

Step 1 of 3

Subquery in WHERE

WHERE id IN (SELECT customer_id FROM orders WHERE total > 1000) — find high-value customers.

Subquery in WHEREDrag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →Subquery in WHEREdrag · tap →Try thisdrag · tap →Follow the dashed drag · tap →

Do this now

Products never ordered: NOT IN or NOT EXISTS pattern.

Was this step clear?
Chapter learning outcomes
  • Subqueries in WHERE
  • Subqueries in FROM
  • WITH ... AS (CTE)
  • EXISTS

Clear these before you leave

Side quest

Month-over-month

CTE: revenue by month, compare to previous month.