Machines With Quick Back-to-Back Runs
Problem
Table `Runs` logs production runs on factory machines: `machine_id`, `run_start` and `run_end` (datetimes), a unique `run_id`, and a `job_type` (such as `Mold` or `Cut`). A run is 'quick back-to-back' when the same machine starts another run of the same `job_type` no more than 8 hours after the previous run of that same type ended.
Find every machine that has at least one quick back-to-back run. Return the distinct `machine_id`, ordered ascending.
Tables
Example rows — the live problem includes the full dataset.
| machine_id | run_start | run_end | run_id | job_type |
|---|---|---|---|---|
| 101 | 2024-03-01 08:00:00 | 2024-03-01 09:00:00 | 1 | Mold |
| 101 | 2024-03-01 10:00:00 | 2024-03-01 11:00:00 | 2 | Cut |
| 102 | 2024-03-01 13:00:00 | 2024-03-01 14:00:00 | 3 | Mold |
Expected output
Your answer should return 1 row with the columns machine_id.
Starter code (SQL)
SELECT *
FROM Runs;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