Module lessons (4/4)
A Query Inside Another: Subqueries
Use the result of one SELECT inside another query with IN, EXISTS, or scalar values.
What you'll learn in this lesson
- Nest a
SELECTinsideWHERE,FROMorSELECTdistinguishing scalar, list (IN) and derived table subqueries. - Choose when to prefer
EXISTSoverIN/NOT INto avoid bugs fromNULLand improve performance. - Recognize the boundaries beyond which it is best to move on to CTE (
WITH).
Overview
Often the filter of a query depends on a value calculated at the moment: "who earns more than the company average?" or "which departments have at least a salary above 50,000?". A subquery (subquery) is a SELECT enclosed in parentheses that produces exactly that intermediate result for the outer query.
See how an intermediate result (subquery or CTE) inserts itself as a logical input at the beginning of the next query:
Sections covered in this lesson
- 11. Mental Model: The 4 Positions of a Subquery
- 22. Progressive & Annotated Examples
- 33. Interactive Laboratory: Composing Intermediate Results
- 44. Diagnostic Verification (Quiz)
- 55. Progressive Practical Laboratory
Unlock "A Query Inside Another: Subqueries" and the full SQL Course curriculum
Use the result of one SELECT inside another query with IN, EXISTS, or scalar values.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in SQL Course plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.