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?

Last updated 13 July 2026

  • Recursion limits vary (SQL Server defaults to 100, set via OPTION (MAXRECURSION n)). On cyclic data, cap depth or track a visited path to avoid infinite loops.

  • Prefer UNION ALL. UNION adds per-iteration dedup that costs performance and can mask missing termination logic.

  • The recursive member may reference the CTE only once, with no aggregates, GROUP BY, DISTINCT, or most subqueries on it. Index join columns since iteration is row-by-row.

  • https://skphd.medium.com/sql-window-functions-interview-questions-and-answers-127ee0be483b
  • https://medium.com/@shreyassn/120-sql-interview-questions-db85e4304ffd
  • https://www.foundit.in/career-advice/20-common-sql-interview-questions-answers/
  • https://medium.com/@Ishan.tharusha.wijayabahu/top-50-essential-sql-interview-questions-for-software-engineers-in-2024-8da12396f199
  • https://interviewkickstart.com/blogs/interview-questions/sql-interview-questions
  • https://medium.com/data-engineer/recursive-ctes-in-sql-7b5dc666b16a
  • https://codewiz.info/blog/sql-common-table-expressions/
  • https://www.dbvis.com/thetable/a-complete-guide-to-the-mysql-cte-mechanism/
All Advanced SQL interview questions