You are given a table Queue representing people waiting to board a bus. The bus has a weight limit of 1000 kilograms.
Each person has:
person_id- unique identifierperson_name- name of the personweight- weight in kilogramsturn- boarding order (1 = first to board)
Write a SQL query to find the name of the last person who can board the bus without exceeding the weight limit of 1000kg.
Note: People board in order of their turn value, and the first person is guaranteed to fit on the bus.
Table Schema
| Column Name | Type | Description |
|---|---|---|
person_id
PK
|
int | Unique identifier for each person |
person_name
|
varchar | Name of the person |
weight
|
int | Weight of person in kilograms |
turn
|
int | Boarding order (1 = first, n = last) |
Input & Output
| person_id | person_name | weight | turn |
|---|---|---|---|
| 5 | Alice | 250 | 1 |
| 4 | Bob | 175 | 5 |
| 3 | Alex | 350 | 2 |
| 6 | John Cena | 400 | 3 |
| 1 | Winston | 500 | 6 |
| 2 | Marie | 200 | 4 |
| person_name |
|---|
| John Cena |
When ordered by turn: Alice (250) → Alex (350) → John Cena (400) → Marie (200) → Bob (175) → Winston (500). Cumulative weights: 250 → 600 → 1000 → 1200. John Cena is the last person whose cumulative weight (1000kg) doesn't exceed the limit.
| person_id | person_name | weight | turn |
|---|---|---|---|
| 1 | Alice | 800 | 1 |
| 2 | Bob | 175 | 2 |
| 3 | Charlie | 80 | 3 |
| person_name |
|---|
| Bob |
Cumulative weights in order: Alice (800) → Bob (975) → Charlie (1055). Since Charlie would exceed 1000kg, Bob is the last person who can board.
Constraints
-
1 ≤ n ≤ 1000where n is the number of people -
1 ≤ person_id ≤ n -
1 ≤ weight ≤ 1000 -
1 ≤ turn ≤ n -
All
person_idandturnvalues are unique - The first person (turn=1) will not exceed the weight limit