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 repeat and build on the results.
Click to reveal answer
intermediate
Why do Recursive CTEs need a termination condition?
To stop the recursion from running forever, a condition limits how many times the recursive part runs, usually by checking when no new rows are added.
Click to reveal answer
beginner
Give a simple real-life example where Recursive CTEs are useful.
Finding all employees under a manager in a company hierarchy, where each employee can have subordinates, and you want to list everyone below a certain manager.
Click to reveal answer
beginner
What SQL keyword is used to define a Recursive CTE?
The keyword WITH is used to start a CTE, and the RECURSIVE keyword (in some SQL dialects like PostgreSQL) indicates that the CTE is recursive.
Click to reveal answer
What does the anchor member of a Recursive CTE do?
✗ Incorrect
The anchor member runs once to provide the initial rows for the recursion.
Which SQL keyword is commonly used to start a Recursive CTE?
✗ Incorrect
WITH starts a CTE, including recursive ones.
Why must a Recursive CTE have a termination condition?
✗ Incorrect
Without a termination condition, recursion would continue forever.
What kind of data is Recursive CTE especially good for?
✗ Incorrect
Recursive CTEs are great for hierarchical data like org charts.
In PostgreSQL, which keyword must be added to WITH to make a CTE recursive?
✗ Incorrect
The RECURSIVE keyword tells PostgreSQL the CTE will call itself.
Explain how a Recursive CTE works step-by-step.
Think of climbing a ladder one step at a time until you reach the top.
You got /4 concepts.
Describe a real-world scenario where you would use a Recursive CTE.
Imagine finding all family members in a family tree.
You got /4 concepts.