Part 1 of 5 · 1 min read
Why this page comes first
In this module you will
- Rewrite a nested subquery as a CTE using WITH.
- Explain why a CTE can make a complex query easier to read, without changing what it computes.
- Write a recursive CTE that walks a parent-child hierarchy.
- Explain the two parts every recursive CTE needs to terminate correctly.
A query with several nested subqueries gets hard to read quickly. A CTE names each piece once, read top to bottom like a short program. This page also covers the one thing a plain subquery cannot do at all: walking a hierarchy of unknown depth.