Coding question · Self Join PairsHardAsked at Google

Mia Jackson's full reporting chain

Recursive CTE for hierarchy traversal.

Back to all coding questions

The Brief

HR AnalyticsOrg design team#coding-interview

Starting with Mia Jackson, return her reporting chain all the way up to the person with no manager. Columns: `depth`, `name`. depth=0 is Mia herself, depth=1 is her manager, and so on. Order by depth. Table: `ci_employees`.

Tables & columns available

ci_employeesdim6 columns
ColumnTypeKey
idINTPK
nameTEXT
department_idINTFK → ci_departments.id
manager_idINTFK → ci_employees.id
salaryINT
hire_dateDATE

Hints (4)

Each hint you reveal reduces the XP you can earn. Try the query first.

Hint 1

WITH RECURSIVE chain AS (anchor UNION ALL recursive_step) SELECT ...

Hint 2

Anchor: WHERE name = 'Mia Jackson' with depth = 0.

Hint 3

Recursive step: JOIN ci_employees e ON e.id = chain.manager_id, depth + 1.

Hint 4

Stop when manager_id is NULL — which happens automatically when the JOIN finds no match.