intermediateCTEs — Common Table Expressions & Recursive CTEs

How does a recursive CTE work?

A recursive CTE has two parts separated by UNION ALL: 1) Anchor member: non-recursive SELECT that returns the starting rows (e.g., employees with no manager). 2) Recursive member: references the CTE itself; joins to find next-level rows (e.g., employees whose manager_id = cte.emp_id). Execution: anchor runs → produces working table W. Recursive member runs with W as input → produces new rows. Repe

This is a Pro chapter

Sign in, then upgrade to Pro or Power to unlock this and the full Databases Mastery library.

How does a recursive CTE work?

Next Step

Continue to What is a writeable CTE in PostgreSQL?← Back to all SQL questions