Standard SQL is "Iterative." But some data structures—like family trees, organizational charts, or file directories—are "Recursive." To query these, we need a query that can call itself. This is a Recursive CTE.
Recursive CTE: A CTE that references itself. It consists of two parts:
- The Anchor Member: The starting point (e.g., the CEO of a company).
- The Recursive Member: The logic that finds the child rows based on the previous row found (e.g., finding the managers reporting to the CEO).
- Org Charts: Printing a list of all employees and their levels (CEO=1, VP=2, Manager=3).
- Path Finding: Finding all stops in a flight itinerary.
- Hierarchy Expansion: Flattening a "Folder > Subfolder > File" structure.
The engine executes WITH RECURSIVE in these steps:
- Execute the Anchor Member. This becomes the "Current Result."
- Execute the Recursive Member using the "Current Result" as an input.
- Append the new matches to the final result set.
- Repeat Step 2-3 using the new matches as the next "Current Result."
- Stop when no new matches are found.
The Infinite Loop Guard: By default, MySQL limits recursion depth to 1000 iterations. This prevents a bug in your SQL from crashing the entire server.
WITH RECURSIVE Hierarchy AS (
-- Anchor: Find the Boss
SELECT Emp_ID, Name, Manager_ID, 1 AS Level
FROM Employees WHERE Manager_ID IS NULL
UNION ALL
-- Recursive: Find everyone reporting to the previous level
SELECT e.Emp_ID, e.Name, e.Manager_ID, h.Level + 1
FROM Employees e
INNER JOIN Hierarchy h ON e.Manager_ID = h.Emp_ID
)
SELECT * FROM Hierarchy;Generating a Number Series (1 to 10):
WITH RECURSIVE Numbers AS (
SELECT 1 AS n -- Start at 1
UNION ALL
SELECT n + 1 FROM Numbers WHERE n < 10 -- Increment until 10
)
SELECT * FROM Numbers;- Forgetting
RECURSIVE: If you try to self-reference a CTE without theRECURSIVEkeyword, MySQL will return an error. - Leaving out the Termination condition: Without a
WHEREclause in the recursive part (liken < 10), the query will run forever until it hits themax_execution_time.
Path Accumulation: You can build a "Breadcrumb" string during recursion to show the full path to a row:
-- Recursive part:
SELECT ..., CONCAT(h.Path, ' > ', e.Name) FROM ...This is how Professional Databases generate "Category Navigation" paths (Electronics > Laptops > Macbooks).
- Task 1: Identify the two main parts of a Recursive CTE and explain their roles.
- Task 2: Write a recursive CTE that prints numbers from
10down to1.