N052-M3 Tier 4 · Advanced · medium hr · Helix Systems

Return the ID and name of every employee who reports to manager `id = 2` at any level of the hierarchy

Part of Recursive CTEs in SQL

The problem

Helix Systems' access control system needs to identify every employee who falls within the reporting chain of manager id = 2, including every level of indirect reports.

Write a query to return the ID and name of every employee who reports to manager id = 2 at any level of the hierarchy.

Assumptions:

  • The employees table has one row per employee with an id, a name, and a manager_id.
  • An employee is a direct report of manager id = 2 if their manager_id equals 2. An employee is an indirect report if their reporting chain leads back to manager id = 2 through one or more intermediate managers.
  • Both direct and indirect reports of manager id = 2 should appear. Manager id = 2 themselves should not appear in the result.

Output:

  • One row per qualifying employee, with columns id and name.
Schema · hr 4 tables
departments
id integer
name text
location text
budget numeric
salaries
id integer
employee_id integer
amount numeric
effective_date date
end_date? date
employees
id integer
name text
email text
department_id integer
manager_id? integer
hire_date date
title text
is_active boolean
job_history
id integer
employee_id integer
title text
department_id integer
start_date date
end_date? date

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution Try it yourself first
Solution query
WITH RECURSIVE
  reports AS (
    SELECT
      id,
      name
    FROM
      employees
    WHERE
      manager_id = 2
    UNION ALL
    SELECT
      e.id,
      e.name
    FROM
      employees e
      JOIN reports r ON e.manager_id = r.id
  )
SELECT
  id,
  name
FROM
  reports

The shape

The anchor seeds the recursion with the direct reports of manager id = 2, not with the manager themselves. The recursive member then walks downward from those seeds, adding each level of indirect reports. Manager id = 2 never enters the CTE, which is exactly what the prompt requires.

Clause by clause

  • The anchor selects every direct report of manager id = 2:
SELECT id, name
FROM employees
WHERE manager_id = 2

The filter manager_id = 2 keeps only the rows whose recorded manager is employee 2. Anna Kim, Ben Walsh, Cara Lee, and Ora Price are the four direct reports; they are the seed set of reports.

  • The recursive member adds each subsequent level by linking on manager_id = id:
UNION ALL
SELECT e.id, e.name
FROM employees e
JOIN reports r ON e.manager_id = r.id

Pass one matches every employee whose manager_id is one of the four direct-report ids and adds them to reports — Sam Torres, Tara Upton, and the rest of the depth-three layer under manager 2. Pass two would look for employees whose manager_id matches one of those newly-added rows, and on this data the subtree terminates there.

  • The main query returns the accumulated set:
SELECT id, name
FROM reports

Fifteen employees in total — the four direct reports plus the eleven indirect reports beneath them. Manager 2 is not in the result, because the anchor never seeded their row.

Why anchor on the direct reports and not on the manager

Seeding the anchor with WHERE id = 2 and then recursing on manager_id = id would include manager 2 in the CTE at depth 1 — fine for a "the manager and everyone beneath" question, but wrong here, where the prompt explicitly excludes the manager. The cleaner fix is to push the exclusion into the anchor: seed with the direct reports (manager_id = 2) and let the recursion handle the rest. The shape of the anchor is what defines who is in the result set.

The trap

The two choices — anchor on the manager or anchor on the direct reports — produce result sets that differ by exactly one row. Either spelling can be right depending on the prompt's wording about whether the root is included. The prompt here calls for "every employee who falls within the reporting chain," which excludes the manager themselves. Reading the inclusion rule off the prompt and writing the anchor to match is the structural step; the recursive member's shape stays the same either way.

You practiced anchoring a WITH RECURSIVE on the direct reports of a specific manager — the anchor restricts the seed set; the recursion expands it down through the subtree without re-including the manager.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.

Practice, feedback, mastery, review. That's the loop that turns reading into real skill.

Start free

No account, no credit card. Start solving in under a minute.