Welcome to Module 04 — Subqueries of the SQL Engineering Handbook. This repository serves as a production-grade open-source technical reference for database platform engineers, query optimization specialists, site reliability engineers (SREs), and data architects.
Rather than teaching basic syntax, this module explores how database cost-based optimizers (CBOs) execute, unnest, decorrelate, and rewrite subqueries under the hood.
Subqueries are nested query blocks evaluated within parent SQL statements. While conceptually straightforward, improper subquery design is a leading cause of production database outages, CPU thread exhaustion, lock contention, and silent data loss caused by 3-valued boolean logic traps.
This handbook module is designed to sit beside PostgreSQL Official Documentation, Microsoft Learn, Oracle Database Docs, and Use The Index, Luke! as a primary engineering reference.
EXPLAIN (ANALYZE, BUFFERS) plan nodes (InitPlan, SubPlan, Hash Semi Join, Hash Anti Join).NULL traps.NOT EXISTS).| Topic | Subject Area | Primary Focus | Markdown Guide | SQL Script |
|---|---|---|---|---|
| 00 | Audit & Setup | Engineering Audit Report & Standalone Executable Database Schema DDL/DML. | Audit | Setup DDL |
| 01 | Single-Row Subqueries | Scalar expressions, InitPlan nodes, scalar comparison operators (=, >, <). |
Guide | SQL |
| 02 | Multi-Row Subqueries | Set membership, IN, ANY, ALL, and work_mem hash materialization. |
Guide | SQL |
| 03 | Correlated Subqueries | Outer column binding, row-by-row execution, SubPlan performance traps. |
Guide | SQL |
| 04 | EXISTS & NOT EXISTS | Semi-Joins ($\ltimes$), Anti-Joins ($\dashv$), short-circuit evaluation, NULL safety. |
Guide | SQL |
| 05 | IN, ANY, & ALL Operators | Quantified operators, formal relational algebra, 3-valued truth tables. | Guide | SQL |
| 06 | Scalar Subqueries | Projected subqueries in SELECT, scalar subquery caching, Window Function rewrites. |
Guide | SQL |
| 07 | Derived Tables | Inline views in FROM/JOIN, subquery pull-up, predicate pushdown. |
Guide | SQL |
| 08 | Subquery Rewrites | Query Rewrite Lab: 6 canonical patterns to eliminate SubPlan nodes. |
Guide | SQL |
| 09 | Subquery Optimization | Benchmark Lab: Scalability statistics across 100K, 1M, 10M, 50M rows. | Guide | SQL |
| 10 | Execution Plans | EXPLAIN (ANALYZE, BUFFERS) tree reading, shared hits, memory spills. |
Guide | SQL |
| 11 | Business Case Studies | 6 end-to-end production solutions (FinTech, E-Commerce, Healthcare). | Guide | SQL |
| 12 | Production Incidents | Production Playbooks: Post-mortems of 5 real-world production outages. | Guide | — |
| 13 | Troubleshooting Guide | Engineering Checklists: Diagnostic flowcharts and 6 deployment checklists. | Guide | — |
| 14 | Interview Guide | Senior/Staff Interview Section: 20 technical questions with structured criteria. | Guide | — |
| 15 | Practice Problems | 15 multi-level enterprise practice problems (Easy, Medium, Hard). | Guide | — |
| 16 | Solutions Suite | Executable, production-tested ANSI SQL solutions for all practice problems. | — | SQL |
Illustrative, not measured. These figures were not captured from an actual benchmark run against provisioned hardware — they’re representative estimates that illustrate the shape of the difference between
O(N × M)correlated re-execution and a decorrelatedO(N + M)hash join, based on the algorithmic complexity discussed in Module 09. Treat the relative pattern (roughly two-orders-of-magnitude gap, growing with scale, culminating in an unrecoverable timeout/OOM for the correlated form) as the takeaway, not the specific millisecond values. If you need real numbers for a capacity-planning decision, run the Benchmark Lab methodology against your own hardware and dataset.
| Scale | Correlated SubPlan Execution | Unnested Hash Semi Join Execution | Performance Gain |
|---|---|---|---|
| 100,000 Rows | ~1,180 ms | ~11 ms | ~100x Faster |
| 1,000,000 Rows | ~17,800 ms | ~88 ms | ~200x Faster |
| 10,000,000 Rows | 🚨 Times out (> 300s) | ~790 ms | Order-of-magnitude+ gap |
| 50,000,000 Rows | 💥 Outage / OOM risk | ~3,920 ms | Correlated form does not survive at this scale |
InitPlan, SubPlan, Hash Semi Join, Parallel Hash, work_mem hash materialization.Materialized_From_Subquery), Semi-Join strategies (FirstMatch, LooseScan).Left Semi Join, Left Anti Semi Join, APPLY operator transformations.AJ) transformations.Contributions are welcome! Please ensure all pull requests follow repository standards:
00_SETUP.sql.2.4.0 (Production Reference Build)Part of the SQL Engineering Handbook • Maintained for Open Source Database Excellence