KatInterview

Daily New, Retained and Churned Devices by Platform

SQL & DataModel answer
MediumModel answerSQL & Datasqlcteretention

Last updated

Write one SQL query that classifies devices as new, retained or churned for 2025-09-01 from a raw event log, and reports net growth overall and per platform.

Daily device growth report

You have a raw event table from a consumer app. Each row is one event emitted by a device.

events(device_id STRING, event_name STRING, ts TIMESTAMP, device_platform STRING)

Build a report for the reporting day 2025-09-01, with 2025-08-31 as the previous day. All dates are calendar dates in UTC. A device is "active" on a day if it has at least one event that day.

Definitions

  • New: a device whose earliest event, across all history, falls on 2025-09-01.
  • Retained: a device active on both 2025-08-31 and 2025-09-01.
  • Churned: a device active on 2025-08-31 but not on 2025-09-01.
  • Net: new + retained - churned.

Output

One row for the whole population and one row per device_platform, with columns:

event_date, device_platform, new_users, retained_users, churn_users, net_users

Use standard SQL with CTEs. Window functions are not required. Count each device once per bucket, even if it has many events.

Sample data (UTC)

device_idevent_nametsdevice_platform
u1app_open2025-08-31T23:50:00Zapplemobile
u1click2025-09-01T01:00:00Zapplemobile
u2app_open2025-08-31T15:00:00Zappletv
u2purchase2025-09-02T03:00:00Zappletv
u3app_open2025-09-01T10:00:00Zdesktop
u4app_open2025-08-31T18:00:00Zdesktop
u5app_open2025-09-01T16:00:00Zapplemobile
u6app_open2025-08-30T12:00:00Zapplemobile
u6app_open2025-08-31T12:00:00Zapplemobile

What to deliver

  1. State the assumptions you need. At minimum, decide how to handle:
    • events after the reporting day,
    • a device that changes platform between the two days,
    • devices that return after a gap (active today, not yesterday, not new).
  2. Write a single query that produces the report.
  3. Show the result you expect on the sample and explain why it is correct.

Hints

Hint 1

Collapse the log to one row per device before counting anything. Once each device appears once, every bucket is a simple yes/no condition.

Hint 2

You need three facts per device: its first-ever active date (over all history), whether it was active on 08-31, and whether it was active on 09-01. A FULL OUTER JOIN between the set of devices active today and the set active yesterday exposes churners, who exist only on the yesterday side.

Hint 3

Decide on one platform label per device so the platform rows add up to the overall row. A reasonable rule is the platform of the latest event today if the device is active today, otherwise the latest event yesterday. Find the latest event with MAX(ts) and join back, instead of a window function. Also remember to exclude events after 2025-09-01.