What is a recursive CTE and when is it used?

SQL subqueries, CTEs and set operations · Freshers and experienced

Model answer

A CTE that refers to itself, with an anchor query and a recursive part combined by UNION ALL, until no new rows appear. It is used for hierarchies such as employee-manager trees, categories and bill of materials. Add a depth limit to avoid endless loops.

Learn this topic

Subqueries, EXISTS and CTEs: Queries Inside Queries in the course SQL for Beginners: Learn Databases Step by Step.

Sign in to add your own answer.

More questions