RANIARANIA Academy

Subqueries & Common Table Expressions

Learn to compose complex SQL queries clearly in PostgreSQL by nesting scalar, row and table subqueries, using IN/EXISTS/ANY/ALL, mastering correlated subqueries, and refactoring tangled logic into readable Common Table Expressions. You will finish able to break a hard reporting question into named, testable steps and even walk hierarchies with recursive CTEs.

Watch the free preview

Scalar subqueries — a query that returns one value — free to watch, no account needed.

PostgreSQL INTERMEDIATE · 150 min · Certificate on completion · 3 CPD points

What you'll learn

  • Compose complex queries clearly by nesting and naming subqueries
  • Choose correctly between scalar, row and table subqueries for a given problem
  • Use IN, EXISTS, ANY and ALL for membership and existence tests without NULL surprises
  • Write correlated subqueries and reason about their per-row execution
  • Refactor tangled SQL into readable CTEs, including recursive CTEs for hierarchies

Syllabus

Subqueries — The Building Blocks
Scalar subqueries — a query that returns one valueFree preview15 min
Row and table subqueries, and subqueries in FROM15 min
Membership & Existence — IN, EXISTS, ANY, ALL
IN and NOT IN — and the NULL trap15 min
EXISTS, ANY and ALL — existence and quantified comparison15 min
Correlated Subqueries
Understanding correlated subqueries15 min
Correlated subqueries, performance and alternatives15 min
Common Table Expressions (CTEs)
WITH — naming steps for readable queries15 min
Recursive CTEs for hierarchies and graphs15 min
Lab — 10 Exercises & Solutions
Exercises 1–515 min
Exercises 6–1015 min