Recall & Review
beginner
What is a Recursive CTE in SQL?
A Recursive CTE (Common Table Expression) is a query that refers to itself to repeatedly execute and build a result set step-by-step, often used to work with hierarchical or tree-structured data.
Click to reveal answer
beginner
What are the two main parts of a Recursive CTE?
1. Anchor member: The starting query that runs once.<br>2. Recursive member: The part that references the CTE itself to add more rows until a condition stops it.
Click to reveal answer
intermediate
How do you stop a Recursive CTE from running forever?
You include a condition in the recursive member that eventually stops adding new rows, preventing infinite loops.
Click to reveal answer
intermediate
Write a simple example of a Recursive CTE to generate numbers from 1 to 5.
WITH RECURSIVE numbers AS (SELECT 1 AS num UNION ALL SELECT num + 1 FROM numbers WHERE num < 5) SELECT * FROM numbers;
Click to reveal answer
beginner
Why are Recursive CTEs useful for hierarchical data?
Because they can repeatedly join a table to itself to find parent-child relationships, making it easy to query trees like organization charts or folder structures.
Click to reveal answer
What keyword starts a Recursive CTE in MySQL?
✗ Incorrect
Recursive CTEs in MySQL start with the keyword 'WITH RECURSIVE'.
Which part of a Recursive CTE runs only once?
✗ Incorrect
The anchor member is the initial query that runs once to start the recursion.
What happens if a Recursive CTE has no stopping condition?
✗ Incorrect
Without a stopping condition, the recursion never ends and causes an error or infinite loop.
Which of these is a valid use case for Recursive CTEs?
✗ Incorrect
Recursive CTEs are great for working with hierarchical data like trees or organizational charts.
In the example: WITH RECURSIVE numbers AS (SELECT 1 AS num UNION ALL SELECT num + 1 FROM numbers WHERE num < 5) SELECT * FROM numbers; What is the output?
✗ Incorrect
The query generates numbers from 1 up to 5 inclusive.
Explain how a Recursive CTE works step-by-step.
Think about how the query builds results like climbing steps.
You got /4 concepts.
Describe a real-life example where Recursive CTEs can be useful.
Imagine a company with managers and employees.
You got /4 concepts.