Last Crate Loaded on the Delivery Van
Problem
Table: `LoadingLine`
```text
+-------------+----------+
| Column | Type |
+-------------+----------+
| crate_id | int |
| label | varchar |
| mass_kg | int |
| load_order | int |
+-------------+----------+
crate_id is the primary key. Crates are loaded onto the van one at a time in
increasing load_order. load_order values are unique.
```
A delivery van can carry at most **1000 kg**. Crates are loaded in `load_order`. As soon as adding the next crate would push the running total above 1000 kg, loading stops and no further crate is loaded.
Return the `label` of the last crate that actually makes it onto the van.
**Example**
Loading in order, the cumulative mass reaches 1000 kg exactly at the third crate, and the fourth would exceed it, so the third crate's label is returned.
Tables
Example rows — the live problem includes the full dataset.
| crate_id | label | mass_kg | load_order |
|---|---|---|---|
| 1 | Tools | 350 | 6 |
| 2 | Linens | 200 | 4 |
| 3 | Books | 350 | 2 |
Expected output
Your answer should return 1 row with the columns label.
Starter code (SQL)
SELECT *
FROM LoadingLine;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