Challenge - 5 Problems
Recursive CTE Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of a simple recursive CTE
Given the following recursive CTE, what will be the output rows?
MySQL
WITH RECURSIVE numbers AS ( SELECT 1 AS n UNION ALL SELECT n + 1 FROM numbers WHERE n < 3 ) SELECT * FROM numbers ORDER BY n;
Attempts:
2 left
💡 Hint
Think about how the recursion stops when n reaches 3.
✗ Incorrect
The CTE starts with 1, then recursively adds 1 until n is less than 3, so it outputs 1, 2, and 3.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in recursive CTE
Which option contains a syntax error in the recursive CTE definition?
Attempts:
2 left
💡 Hint
Look carefully at the recursive SELECT statement syntax.
✗ Incorrect
Option C is missing the FROM keyword before 'cte' in the recursive SELECT, causing a syntax error.
❓ optimization
advanced2:00remaining
Optimizing recursive CTE for performance
Which option best improves performance of this recursive CTE that generates a sequence of numbers up to 1000?
MySQL
WITH RECURSIVE seq AS ( SELECT 1 AS num UNION ALL SELECT num + 1 FROM seq WHERE num < 1000 ) SELECT * FROM seq;
Attempts:
2 left
💡 Hint
Think about alternatives to recursion for generating sequences.
✗ Incorrect
Using a pre-built numbers table is more efficient than recursive CTE for large sequences.
🧠 Conceptual
advanced2:00remaining
Understanding termination condition in recursive CTEs
Why is it important to have a proper termination condition in a recursive CTE?
Attempts:
2 left
💡 Hint
Think about what happens if recursion never stops.
✗ Incorrect
Without a termination condition, recursion never ends, causing infinite loops and resource exhaustion.
🔧 Debug
expert3:00remaining
Debugging unexpected output in recursive CTE
Given this recursive CTE, why does it output duplicate rows?
MySQL
WITH RECURSIVE cte AS ( SELECT 1 AS val UNION ALL SELECT val + 1 FROM cte WHERE val < 3 UNION ALL SELECT val + 1 FROM cte WHERE val < 3 ) SELECT * FROM cte ORDER BY val;
Attempts:
2 left
💡 Hint
Look at how many recursive SELECTs are combined with UNION ALL.
✗ Incorrect
Having two UNION ALL recursive SELECTs adds the same rows twice, causing duplicates.