Most Recent Check-In per Locker
Problem
Table: `locker_use`
| Column Name | Type |
|---|---|
| session_id | int |
| locker_tag | varchar |
| opened_at | datetime |
| closed_at | datetime |
`session_id` is the primary key. Each row is one rental session of a gym locker, with the moment it was opened and closed.
For every locker, find the latest moment it was closed. Output `locker_tag` and that latest close time aliased as `closed_at`, ordered by `closed_at` descending.
Tables
Example rows — the live problem includes the full dataset.
| session_id | locker_tag | opened_at | closed_at |
|---|---|---|---|
| 1 | LK-204 | 2023-07-10 08:00:00 | 2023-07-10 09:15:00 |
| 2 | LK-118 | 2023-07-10 07:30:00 | 2023-07-10 07:55:00 |
| 3 | LK-204 | 2023-07-11 18:00:00 | 2023-07-11 19:40:00 |
Expected output
Your answer should return 2 rows with the columns locker_tag, closed_at.
Starter code (SQL)
SELECT *
FROM locker_use;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