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

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

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