AnalystPath

Devices With the Longest Run of Daily Check-ins

SQLHardSenior level~15 min

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.

thermostat_pings
ping_iddevice_idping_dayenergy_kwh
11012024-03-015
21012024-03-026
31012024-03-034

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

Related SQL questions