Bird
0
0

Identify the error in this recursive CTE:

medium📝 Debug Q6 of 15
SQL - Common Table Expressions (CTEs)
Identify the error in this recursive CTE:
WITH RECURSIVE cte AS (SELECT 1 AS n UNION SELECT n + 1 FROM cte WHERE n < 5) SELECT * FROM cte;
AIncorrect base SELECT syntax
BCTE name is invalid
CMissing UNION ALL keyword causes infinite recursion
DWHERE clause should be outside the CTE
Step-by-Step Solution
Solution:
  1. Step 1: Check recursive CTE syntax

    Recursive CTEs require UNION ALL to combine base and recursive parts to avoid infinite loops.
  2. Step 2: Identify missing UNION ALL

    This query uses UNION instead of UNION ALL, causing the recursion to not terminate properly.
  3. Final Answer:

    Missing UNION ALL keyword causes infinite recursion -> Option C
  4. Quick Check:

    Use UNION ALL in recursive CTEs [OK]
Quick Trick: Recursive CTEs must use UNION ALL, not UNION [OK]
Common Mistakes:
  • Using UNION instead of UNION ALL
  • Placing WHERE outside CTE
  • Misnaming CTE

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes