How Many Veteran and Rookie Pilots Fit the Payroll
Problem
Table: `pilots`
| Column Name | Type |
|---|---|
| pilot_id | int |
| rank | enum |
| wage | int |
`pilot_id` is the primary key. `rank` is one of `('Veteran', 'Rookie')`. `wage` is the pilot's annual wage.
An airline has a hiring budget of `50000`. It first hires `Veteran` pilots, cheapest wage first (ties broken by smaller `pilot_id`), taking each as long as the running total of veteran wages stays within budget. With whatever budget remains, it then hires `Rookie` pilots under the same cheapest-first rule. Report two rows with columns `rank` and `hired_count`: the number of veterans hired and the number of rookies hired. Return the veteran row first.
Tables
Example rows — the live problem includes the full dataset.
| pilot_id | rank | wage |
|---|---|---|
| 1 | Veteran | 20000 |
| 2 | Veteran | 25000 |
| 3 | Veteran | 30000 |
Expected output
Your answer should return 2 rows with the columns rank, hired_count.
Starter code (SQL)
SELECT *
FROM pilots;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