Skip to content

Commit 5d31c0b

Browse files
committed
Update 00-cte.md
1 parent 3cb5982 commit 5d31c0b

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

docs/en/guides/54-query/00-cte.md

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -293,37 +293,32 @@ Now, we use a recursive CTE to find the hierarchy of employees under a specific
293293

294294
```sql
295295
WITH RECURSIVE EmployeeHierarchy AS (
296-
-- Initial query: start with the specified manager (Alice)
296+
-- Start with Alice (the CEO)
297297
SELECT EmployeeID, EmployeeName, ManagerID
298298
FROM Employees
299-
WHERE ManagerID IS NULL -- Alice, since she has no manager
299+
WHERE ManagerID IS NULL
300300
UNION ALL
301-
-- Recursive query: find employees reporting to the current level
301+
-- Recursively find employees reporting to the current level
302302
SELECT e.EmployeeID, e.EmployeeName, e.ManagerID
303303
FROM Employees e
304-
INNER JOIN EmployeeHierarchy AS eh2 ON e.ManagerID = eh2.EmployeeID
304+
JOIN EmployeeHierarchy eh2 ON e.ManagerID = eh2.EmployeeID
305305
)
306-
SELECT eh.EmployeeID, eh.EmployeeName, eh.ManagerID, m.EmployeeName AS ManagerName
306+
SELECT eh.EmployeeID, eh.EmployeeName, m.EmployeeName AS ManagerName
307307
FROM EmployeeHierarchy eh
308-
LEFT JOIN Employees m ON eh.ManagerID = m.EmployeeID
309-
ORDER BY CASE
310-
WHEN eh.EmployeeName = 'Alice' THEN 0 -- Alice goes first
311-
ELSE 1
312-
END,
313-
eh.EmployeeID; -- Then order by EmployeeID
308+
JOIN Employees m ON eh.ManagerID = m.EmployeeID
309+
ORDER BY eh.EmployeeID;
314310
```
315311

316312
The output will list all employees in the hierarchy under Alice:
317313

318314
```sql
319-
┌─────────────────────────────────────────────────────────────────────────┐
320-
│ employeeid │ employeename │ managerid │ managername │
321-
├─────────────────┼──────────────────┼─────────────────┼──────────────────┤
322-
1 │ Alice │ NULLNULL
323-
2 │ Bob │ 1 │ Alice │
324-
3 │ Charlie │ 1 │ Alice │
325-
4 │ David │ 2 │ Bob │
326-
5 │ Eve │ 2 │ Bob │
327-
6 │ Frank │ 3 │ Charlie │
328-
└─────────────────────────────────────────────────────────────────────────┘
315+
┌───────────────────────────────────────────────────────┐
316+
│ employeeid │ employeename │ managername │
317+
├─────────────────┼──────────────────┼──────────────────┤
318+
2 │ Bob │ Alice │
319+
3 │ Charlie │ Alice │
320+
4 │ David │ Bob │
321+
5 │ Eve │ Bob │
322+
6 │ Frank │ Charlie │
323+
└───────────────────────────────────────────────────────┘
329324
```

0 commit comments

Comments
 (0)