0
0
MySQLquery~20 mins

Recursive CTEs in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Recursive CTE Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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;
A1, 2, 3
B1, 2, 3, 4
C1, 2
DSyntax error
Attempts:
2 left
💡 Hint
Think about how the recursion stops when n reaches 3.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in recursive CTE
Which option contains a syntax error in the recursive CTE definition?
AWITH RECURSIVE cte AS (SELECT 1 AS val UNION ALL SELECT val + 1 FROM cte WHERE val < 5) SELECT * FROM cte;
BWITH RECURSIVE cte AS (SELECT 1 AS val UNION ALL SELECT val + 1 FROM cte WHERE val <= 5) SELECT * FROM cte;
CWITH RECURSIVE cte AS (SELECT 1 AS val UNION ALL SELECT val + 1 cte WHERE val < 5) SELECT * FROM cte;
D;etc MORF * TCELES )5 < lav EREHW etc MORF 1 + lav TCELES LLA NOINU lav SA 1 TCELES( SA etc EVISRUCER HTIW
Attempts:
2 left
💡 Hint
Look carefully at the recursive SELECT statement syntax.
optimization
advanced
2: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;
AAdd an index on the CTE column 'num' to speed recursion
BLimit recursion depth by adding OPTION (MAXRECURSION 1000)
CRewrite the CTE to use a JOIN instead of UNION ALL
DUse a non-recursive numbers table instead of recursive CTE
Attempts:
2 left
💡 Hint
Think about alternatives to recursion for generating sequences.
🧠 Conceptual
advanced
2:00remaining
Understanding termination condition in recursive CTEs
Why is it important to have a proper termination condition in a recursive CTE?
ATo prevent infinite recursion and possible server crash
BTo make the query run faster by skipping recursion
CTo allow the CTE to run multiple times in parallel
DTo enable the CTE to return duplicate rows
Attempts:
2 left
💡 Hint
Think about what happens if recursion never stops.
🔧 Debug
expert
3: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;
ABecause the CTE is missing an ORDER BY clause inside
BBecause the recursive part has two UNION ALL clauses causing duplicates
CBecause the WHERE condition is incorrect and allows duplicates
DBecause the base case SELECT is missing a DISTINCT keyword
Attempts:
2 left
💡 Hint
Look at how many recursive SELECTs are combined with UNION ALL.