Most Recent 2022 Backup
Problem
A cloud storage app logs every time a device uploads a backup.
Table: backups
| Column Name | Type |
|-------------|----------|
| device_id | int |
| run_at | datetime |
(device_id, run_at) is the primary key for this table. Each row records one backup run by a device at a given moment.
Write a query that reports, for every device that ran at least one backup during the year 2022, the timestamp of its most recent 2022 backup. Devices with no 2022 backup must not appear.
Return the result in any order.
Tables
Example rows — the live problem includes the full dataset.
| device_id | run_at |
|---|---|
| 6 | 2022-06-30 15:06:07 |
| 6 | 2023-04-21 14:06:06 |
| 6 | 2021-03-07 00:18:15 |
Expected output
Your answer should return 3 rows with the columns device_id, latest_run.
Starter code (SQL)
SELECT *
FROM backups;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