AnalystPath

Most Recent Check-In per Locker

SQLEasyJunior level~15 min

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.

locker_use
session_idlocker_tagopened_atclosed_at
1LK-2042023-07-10 08:00:002023-07-10 09:15:00
2LK-1182023-07-10 07:30:002023-07-10 07:55:00
3LK-2042023-07-11 18:00:002023-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

Related SQL questions