Devices With the Longest Run of Daily Check-ins
Problem
Table: `thermostat_pings`
| Column Name | Type |
|---|---|
| ping_id | int |
| device_id | int |
| ping_day | date |
| energy_kwh | int |
`ping_id` is the primary key. Each row records that a smart thermostat checked in once on a given calendar day. A device checks in at most once per day.
A "run" is a maximal stretch of consecutive calendar days on which a device checked in (no gaps). Find the device(s) whose longest run is the longest among all devices. Return the `device_id` of each such device, ordered by `device_id`.
Tables
Example rows — the live problem includes the full dataset.
| ping_id | device_id | ping_day | energy_kwh |
|---|---|---|---|
| 1 | 101 | 2024-03-01 | 5 |
| 2 | 101 | 2024-03-02 | 6 |
| 3 | 101 | 2024-03-03 | 4 |
Expected output
Your answer should return 2 rows with the columns device_id.
Starter code (SQL)
SELECT *
FROM thermostat_pings;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