AnalystPath

Everyone Under the Squadron Commander

SQLMediumMid level~15 min

Problem

Table: `Officers`

```text
+--------------+---------+
| Column | Type |
+--------------+---------+
| officer_id | int |
| officer_name | varchar |
| superior_id | int |
+--------------+---------+
officer_id is the primary key. Each row says the officer reports to the
officer whose id is superior_id. The squadron commander is officer_id = 1
(the commander's superior_id is their own id).
```

Find the `officer_id` of every officer who reports to the squadron commander (id = 1) **directly or indirectly**. Indirect means a chain: A reports to B, B reports to C, and C reports to the commander. The chain of command is at most 3 levels deep below the commander.

Do not include the commander themself. Return `officer_id` in any order.

Tables

Example rows — the live problem includes the full dataset.

Officers
officer_idofficer_namesuperior_id
1Reyes1
2Singh1
3Tariq3

Expected output

Your answer should return 4 rows with the columns officer_id.

Starter code (SQL)

SELECT *
FROM Officers;

Solve this SQL question free

Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related SQL questions