Skip to main content
Module 4 · Lesson 4 of 416/57 in the course~15 min
Module lessons (4/4)

A Query Inside Another: Subqueries

Lesson preview5 hands-on exercises readySQL Course · Module 4 — Practical SQL

Use the result of one SELECT inside another query with IN, EXISTS, or scalar values.

What you'll learn in this lesson

  • Nest a SELECT inside WHERE, FROM or SELECT distinguishing scalar, list (IN) and derived table subqueries.
  • Choose when to prefer EXISTS over IN / NOT IN to avoid bugs from NULL and 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

  1. 11. Mental Model: The 4 Positions of a Subquery
  2. 22. Progressive & Annotated Examples
  3. 33. Interactive Laboratory: Composing Intermediate Results
  4. 44. Diagnostic Verification (Quiz)
  5. 55. Progressive Practical Laboratory
Pro Lesson · Module 4 — Practical SQL

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.