Exercise 1: Recursive CTEs and Why MySQL Lacked Them Before 8.0 — Possible Solution ==================================================================== WHAT A RECURSIVE CTE IS ------------------------------ Per this chapter, "a recursive CTE, using WITH RECURSIVE, lets a query reference itself, building up a result iteratively — the standard way to traverse a hierarchical structure... whose depth isn't known in advance." It has two parts unioned with UNION ALL: an anchor term (the starting point) and a recursive term that "references the CTE's own name and is executed repeatedly until it returns no more rows." USING THIS CHAPTER'S OWN EMPLOYEE HIERARCHY EXAMPLE ------------------------------ Per this chapter's own worked example: WITH RECURSIVE org_chart AS ( SELECT id, name, manager_id, 0 AS depth FROM employees WHERE id = 7 UNION ALL SELECT e.id, e.name, e.manager_id, org_chart.depth + 1 FROM employees e JOIN org_chart ON e.manager_id = org_chart.id ) SELECT * FROM org_chart ORDER BY depth, name; The anchor term finds manager 7 alone, at depth 0. The recursive term then finds every employee whose manager_id matches an id ALREADY found in org_chart, adds them at depth+1, and this repeats — first finding manager 7's direct reports, then their reports, and so on — until a pass finds no new employees to add, at which point the recursion stops. The final result includes every employee under manager 7 at any depth, without the query needing to know in advance how many levels deep the hierarchy actually goes. WHY MYSQL COULDN'T DO THIS IN PURE SQL BEFORE 8.0 ------------------------------ Per this chapter, "MySQL only added recursive CTE support in MySQL 8.0, released in 2018. Before that, this exact query had no equivalent in pure SQL at all — it required either application-level recursion or a stored-procedure loop." Without WITH RECURSIVE, there was no SQL-native mechanism for a query to reference its own, still-growing result set. Achieving the same "find everyone under manager 7 at any depth" result meant writing actual procedural code — either looping in the application itself (running repeated queries, one level at a time, until no new results appeared) or writing a stored procedure with an explicit loop — a meaningfully heavier, more error-prone approach than a single declarative SQL query. WHY THIS WORKS AS AN ANSWER ------------------------------ It explains the anchor/recursive mechanism using the chapter's own worked example step by step, and states specifically what alternative MySQL required before 8.0, rather than simply saying "MySQL added this feature later."