Everyone Under the Squadron Commander
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.
| officer_id | officer_name | superior_id |
|---|---|---|
| 1 | Reyes | 1 |
| 2 | Singh | 1 |
| 3 | Tariq | 3 |
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