Bird
Raised Fist0
MySQLquery~10 mins

Recursive CTEs in MySQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Recursive CTEs
Start Recursive CTE
↓
Anchor Member: Base Query
↓
Recursive Member: Query referencing CTE
↓
Union All: Combine Anchor + Recursive
↓
Repeat Recursive Member with new rows
↓
Stop when Recursive Member returns no rows
↓
Final Result: All combined rows
A recursive CTE starts with a base query, then repeatedly runs a recursive query that references itself, combining results until no new rows appear.
Execution Sample
MySQL
WITH RECURSIVE nums AS (
  SELECT 1 AS n
  UNION ALL
  SELECT n + 1 FROM nums WHERE n < 3
)
SELECT * FROM nums;
This query generates numbers from 1 to 3 using a recursive CTE.
Execution Table
StepActionAnchor ResultRecursive ResultCombined Result
1Run anchor member[1][][1]
2Run recursive member with n=1[][2][1, 2]
3Run recursive member with n=2[][3][1, 2, 3]
4Run recursive member with n=3[][][1, 2, 3]
💡 Recursive member returns no rows at step 4, so recursion stops.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
nundefined1233
Key Moments - 3 Insights
Why does the recursion stop at step 4?
Because the recursive member returns no new rows (empty set) at step 4, so there is nothing more to add to the combined result, as shown in execution_table row 4.
What is the role of the anchor member?
The anchor member provides the starting rows for the recursion. In the example, it returns the initial value 1 at step 1, which seeds the recursive process.
How does the recursive member reference the CTE itself?
The recursive member queries the CTE 'nums' inside its own definition, allowing it to build on previous results, as seen in the recursive SELECT n + 1 FROM nums.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the combined result after step 2?
A[1]
B[2]
C[1, 2]
D[1, 2, 3]
💡 Hint
Check the 'Combined Result' column in execution_table row 2.
At which step does the recursive member return no rows?
AStep 4
BStep 3
CStep 1
DStep 2
💡 Hint
Look at the 'Recursive Result' column in execution_table; empty set appears at step 4.
If the anchor member returned 2 instead of 1, what would be the first combined result?
A[1]
B[2]
C[3]
D[]
💡 Hint
Anchor member result is the first combined result; see execution_table step 1.
Concept Snapshot
WITH RECURSIVE cte_name AS (
  anchor_query
  UNION ALL
  recursive_query_referencing_cte
)
SELECT * FROM cte_name;

- Anchor query runs once to start.
- Recursive query runs repeatedly, adding rows.
- Stops when recursive query returns no rows.
- Useful for hierarchical or iterative data generation.
Full Transcript
Recursive CTEs in MySQL start with an anchor query that provides initial rows. Then a recursive query runs repeatedly, referencing the CTE itself to add new rows. This repeats until the recursive query returns no rows, combining all results. For example, generating numbers 1 to 3 starts with 1, then adds 2, then 3, and stops when no more numbers are added. This process is shown step-by-step in the execution table and variable tracker. Understanding when recursion stops and how the anchor seeds the process helps beginners grasp recursive CTEs.

Practice

(1/5)
1. What is the main purpose of a recursive CTE in MySQL?
easy
A. To create temporary tables for storing large datasets
B. To repeatedly execute a query to explore hierarchical or connected data
C. To speed up simple SELECT queries without joins
D. To permanently store data in the database

Solution

  1. Step 1: Understand recursive CTE concept

    A recursive CTE runs a base query and then repeatedly runs a recursive part referring back to itself to explore connected data.
  2. Step 2: Identify the purpose

    This process is mainly used to handle hierarchical or tree-like data, such as organizational charts or folder structures.
  3. Final Answer:

    To repeatedly execute a query to explore hierarchical or connected data -> Option B
  4. Quick Check:

    Recursive CTE = hierarchical data exploration [OK]
Hint: Recursive CTEs repeat queries to explore connected data [OK]
Common Mistakes:
  • Confusing recursive CTEs with temporary tables
  • Thinking recursive CTEs speed up all queries
  • Assuming recursive CTEs permanently store data
2. Which of the following is the correct basic syntax to start a recursive CTE in MySQL?
easy
A. WITH cte_name RECURSIVE (anchor_query UNION recursive_query) SELECT * FROM cte_name;
B. CREATE RECURSIVE TABLE cte_name AS (anchor_query UNION recursive_query);
C. WITH RECURSIVE cte_name AS (anchor_query UNION ALL recursive_query) SELECT * FROM cte_name;
D. SELECT RECURSIVE * FROM cte_name WHERE condition;

Solution

  1. Step 1: Recall MySQL recursive CTE syntax

    MySQL uses the keyword WITH RECURSIVE followed by the CTE name and a query combining an anchor and recursive part with UNION ALL.
  2. Step 2: Match syntax options

    WITH RECURSIVE cte_name AS (anchor_query UNION ALL recursive_query) SELECT * FROM cte_name; correctly shows this syntax, including UNION ALL and the final SELECT from the CTE.
  3. Final Answer:

    WITH RECURSIVE cte_name AS (anchor_query UNION ALL recursive_query) SELECT * FROM cte_name; -> Option C
  4. Quick Check:

    WITH RECURSIVE + UNION ALL = correct syntax [OK]
Hint: Look for WITH RECURSIVE and UNION ALL keywords [OK]
Common Mistakes:
  • Using UNION instead of UNION ALL in recursive part
  • Trying to CREATE RECURSIVE TABLE (not valid)
  • Misplacing RECURSIVE keyword after CTE name
3. Given the table employees(id, manager_id, name), what will this query return?
WITH RECURSIVE subordinates AS (
  SELECT id, name FROM employees WHERE id = 2
  UNION ALL
  SELECT e.id, e.name FROM employees e
  JOIN subordinates s ON e.manager_id = s.id
)
SELECT * FROM subordinates;
medium
A. All employees who report directly or indirectly to employee with id 2
B. Only the employee with id 2
C. All employees who manage employee with id 2
D. Syntax error due to missing anchor query

Solution

  1. Step 1: Analyze anchor query

    The anchor selects the employee with id = 2 as the starting point.
  2. Step 2: Analyze recursive part

    The recursive part joins employees whose manager_id matches the current subordinates' id, thus finding direct reports, then their reports, and so on.
  3. Step 3: Understand final output

    The query returns employee 2 and all employees reporting to them directly or indirectly.
  4. Final Answer:

    All employees who report directly or indirectly to employee with id 2 -> Option A
  5. Quick Check:

    Recursive join on manager_id finds all subordinates [OK]
Hint: Recursive join on manager_id finds all subordinates [OK]
Common Mistakes:
  • Confusing manager and subordinate roles
  • Thinking it returns only direct reports
  • Assuming syntax error due to anchor query presence
4. Identify the error in this recursive CTE:
WITH RECURSIVE cte AS (
  SELECT id FROM items WHERE parent_id IS NULL
  UNION
  SELECT i.id FROM items i JOIN cte ON i.parent_id = cte.id
)
SELECT * FROM cte;
medium
A. Anchor query should use WHERE parent_id = 0 instead of IS NULL
B. Recursive part should use LEFT JOIN instead of JOIN
C. CTE name cannot be 'cte', must be more descriptive
D. Missing UNION ALL causes infinite recursion or wrong results

Solution

  1. Step 1: Check recursive CTE syntax

    Recursive CTEs require UNION ALL to combine anchor and recursive parts to avoid duplicate elimination.
  2. Step 2: Understand effect of UNION vs UNION ALL

    Using UNION removes duplicates each iteration, which can cause infinite recursion or incorrect results in recursive queries.
  3. Final Answer:

    Missing UNION ALL causes infinite recursion or wrong results -> Option D
  4. Quick Check:

    Recursive CTEs must use UNION ALL [OK]
Hint: Always use UNION ALL in recursive CTEs to avoid infinite loops [OK]
Common Mistakes:
  • Using UNION instead of UNION ALL in recursive CTE
  • Assuming NULL vs 0 matters for recursion logic
  • Thinking JOIN type affects recursion correctness
5. You want to list all ancestors of an employee in a company hierarchy stored in employees(id, manager_id, name). Which recursive CTE query correctly finds all managers up the chain for employee with id 5?
hard
A. WITH RECURSIVE ancestors AS ( SELECT id, manager_id, name FROM employees WHERE id = 5 UNION ALL SELECT e.id, e.manager_id, e.name FROM employees e JOIN ancestors a ON e.id = a.manager_id ) SELECT * FROM ancestors WHERE id != 5;
B. WITH RECURSIVE ancestors AS ( SELECT id, manager_id, name FROM employees WHERE id = 5 UNION ALL SELECT e.id, e.manager_id, e.name FROM employees e JOIN ancestors a ON e.manager_id = a.id ) SELECT * FROM ancestors WHERE id != 5;
C. WITH RECURSIVE ancestors AS ( SELECT id, manager_id, name FROM employees WHERE manager_id = 5 UNION ALL SELECT e.id, e.manager_id, e.name FROM employees e JOIN ancestors a ON e.manager_id = a.id ) SELECT * FROM ancestors;
D. WITH RECURSIVE ancestors AS ( SELECT id, manager_id, name FROM employees WHERE id = 5 UNION SELECT e.id, e.manager_id, e.name FROM employees e JOIN ancestors a ON e.id = a.manager_id ) SELECT * FROM ancestors WHERE id != 5;

Solution

  1. Step 1: Understand the goal

    We want to find all managers (ancestors) of employee 5, moving up the chain via manager_id.
  2. Step 2: Analyze join condition

    To move up, join employees where employee id matches the manager_id of current ancestor (e.id = a.manager_id).
  3. Step 3: Check anchor and recursive parts

    Anchor selects employee 5; recursive part finds their manager, then that manager's manager, etc.
  4. Final Answer:

    WITH RECURSIVE ancestors AS (SELECT id, manager_id, name FROM employees WHERE id = 5 UNION ALL SELECT e.id, e.manager_id, e.name FROM employees e JOIN ancestors a ON e.id = a.manager_id) SELECT * FROM ancestors WHERE id != 5; -> Option A
  5. Quick Check:

    Join on e.id = a.manager_id moves up hierarchy [OK]
Hint: Join on e.id = a.manager_id to move up the hierarchy [OK]
Common Mistakes:
  • Reversing join condition to move down instead of up
  • Using UNION instead of UNION ALL causing errors
  • Starting anchor with manager_id = 5 instead of id = 5
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard