This is famous Microsoft sample database Northwind
(immutable since 1997). The Emploeees
table demonstrates hierarchical data and usage of recursive CTE (not available in 1997) to access it.
For better understanding / testing / training purpose add an extra column
WITH Managers AS
(
--initialization
SELECT EmployeeID, LastName, ReportsTo, /*extra column*/ 0 [level]
FROM Employees
WHERE ReportsTo IS NULL
UNION ALL
--recursive execution
SELECT e.employeeID,e.LastName, e.ReportsTo, [level]+1
FROM Employees e
INNER JOIN Managers m ON e.ReportsTo = m.employeeID
)
SELECT * FROM Managers
This is result set.
EmployeeID LastName ReportsTo level
----------- -------------------- ----------- -----------
2 Fuller NULL 0 --anchor call
--now 'Managers' CTE is the anchor (level 0)
1 Davolio 2 1
3 Leverling 2 1
4 Peacock 2 1
5 Buchanan 2 1
8 Callahan 2 1
--now 'Managers' CTE is resultset level 1
6 Suyama 5 2
7 King 5 2
9 Dodsworth 5 2
So the anchor executes always, recursive part executes iff the anchor returns row(s).