▸_sqlgym

66. Everyone Who Reports Up To A Manager

HardRecursive CTEsRecursive CTETransitive ClosureGraph

A recursive CTE walks a hierarchy: the anchor picks the starting rows, and the recursive member repeatedly joins the table back onto the rows found so far until nothing new appears. This computes the transitive closure — every node reachable from the start.

The employees table is a self-referencing tree: manager_id points at another employee's id (the CEO has NULL).

Task: list everyone who reports up to Alice (id 1), directly or indirectly — i.e. her whole sub-tree, excluding Alice herself. Also report each person's depth below Alice (direct reports = 1).

Return columns name, depth, ordered by depth ascending, then name ascending.

Tables
Loading schema…
Expected outputrows must come back in this order
Computing expected output…

This is the result for the example data above. On Submit your query is graded against this example plus 4 hidden edge cases 5test cases in all. A sloppy query that only fits the example won't pass.

SQL
⌘/Ctrl+↵ run · ⇧⌘/Ctrl+↵ submit
Run a query to see its output here.