KatInterview

Daily KPI Pipeline and Retention Cohorts in SQL

SQL & DataModel answer
HardModel answerSQL & Datasqlwindow-functionstimezones

Last updated

Write SQL for a photo app: totals, top month, top followed user, daily KPI and follower-bucket tables in Los Angeles time, and week-1 retention cohorts.

Scenario

You own the analytics layer for a photo-sharing app. The raw tables are:

  • users(user_id, user_name, joined_at)
  • photos(photo_id, user_id, created_at)
  • follows(follower_user_id, followed_user_id, created_at)
  • user_events(event_id, user_id, event_type, occurred_at)

Rules:

  • Every timestamp is stored in UTC.
  • Unless told otherwise, a "day" or "month" means a calendar day or month in America/Los_Angeles.
  • A follows row means the follow began at created_at and stays active from then on. Nobody unfollows.
  • DAU for a day is the number of distinct users with at least one user_events row on that local day.

A dashboard reads from three pre-aggregated tables that you must fill:

  • kpi_daily_base(report_date, new_users, total_users_eod, dau, new_photos, total_follow_edges_eod)
  • follower_bucket_daily(report_date, bucket, user_count), where bucket is '0', '1_2' or '3_plus'
  • retention_cohort_daily(cohort_date, cohort_size, retained_w1_users)

Tasks

  1. Return the total number of users.
  2. Return the calendar month with the most signups.
  3. Return the user with the most followers.
  4. Populate the daily tables so the dashboard can show, per day:
    • new users, and the change vs. 7 days earlier
    • DAU, and the change vs. 7 days earlier
    • new photos
    • new photos per DAU, and the change vs. 7 days earlier
    • total follower relationships
    • the share of users with 0 followers, with 1-2 followers, and with 3+ followers
  5. Extend the work to compute week-1 retention. For users who signed up on local day D, this is the share with at least one event on any local day from D+7 to D+13.

Write the SQL (any mainstream dialect is fine; say which one you use) and explain the design choices that keep the numbers correct.

Hints

Hint 1

Convert from UTC to the reporting timezone first, then cast to a date or truncate to a month. Truncating UTC and converting afterwards puts evening Pacific events on the wrong day.

Hint 2

Build a gapless list of dates (a date spine) and LEFT JOIN each daily count onto it with COALESCE to 0. Then 'row 7 back' really means 'the same weekday last week', and running totals are just window sums over the spine.

Hint 3

Follower counts never go down, so each user crosses the 1-follower and 3-follower thresholds exactly once. Find those two dates per user, count them per day, run a cumulative sum, and derive the three buckets by subtraction from total users.

Hint 4

For retention, deduplicate (user, active_date) pairs, then LEFT JOIN them to each user's signup date with the condition active_date BETWEEN cohort_date + 7 AND cohort_date + 13. Count distinct matched users against cohort size.