Pitfalls of recursive CTEs: recursion limits, UNION ALL vs UNION, and the single self-reference rule

Quick Overview

Asks for the limitations and common pitfalls of recursive CTEs in SQL, including recursion limits, the choice between UNION ALL and UNION, and the restrictions on the recursive member. Tests how well you understand recursive evaluation, cycle handling and the differences between database engines.

Pitfalls of recursive CTEs: recursion limits, UNION ALL vs UNION, and the single self-reference rule

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

What are the limitations or common pitfalls of recursive CTEs, such as max recursion limit, `UNION ALL` vs `UNION`, and single reference in the recursive member? ```hint Trace a small case Run a recursive CTE by hand on a parent-child table that contains a cycle. On each iteration, which rows can the recursive member see, and what makes it stop? ``` ### What a Strong Answer Covers - How recursion terminates, what happens on cyclic data, and how engines cap (or fail to cap) recursion depth - What `UNION` changes compared with `UNION ALL`, and which engines accept each - The restrictions on the recursive member and the evaluation model that explains them - Practical guards against runaway queries, such as a depth counter, a visited path or a cycle clause - Performance and typing traps, such as unindexed join columns and column types fixed by the anchor ### Follow-up Questions - How would you find and report the cycles in a parent-child table instead of only stopping on them? - You need a transitive closure where the obvious recursive member joins the CTE to itself. How do you write it within the single-reference rule? - How do you return a hierarchy in depth-first order with each node's full path? - When would you store a hierarchy as a closure table or nested sets instead of querying it with a recursive CTE?

Overview: Asks for the limitations and common pitfalls of recursive CTEs in SQL, including recursion limits, the choice between UNION ALL and UNION, and the restrictions on the recursive member. Tests how well you understand recursive evaluation, cycle handling and the differences between database engines.

PracHub logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

What are the limitations or common pitfalls of recursive CTEs, such as max recursion limit, UNION ALL vs UNION, and single reference in the recursive member?

What a Strong Answer Covers Guidance

  • How recursion terminates, what happens on cyclic data, and how engines cap (or fail to cap) recursion depth
  • What UNION changes compared with UNION ALL , and which engines accept each
  • The restrictions on the recursive member and the evaluation model that explains them
  • Practical guards against runaway queries, such as a depth counter, a visited path or a cycle clause
  • Performance and typing traps, such as unindexed join columns and column types fixed by the anchor

Follow-up Questions Guidance

  • How would you find and report the cycles in a parent-child table instead of only stopping on them?
  • You need a transitive closure where the obvious recursive member joins the CTE to itself. How do you write it within the single-reference rule?
  • How do you return a hierarchy in depth-first order with each node's full path?
  • When would you store a hierarchy as a closure table or nested sets instead of querying it with a recursive CTE?