KatInterview

Time-Zone-Aware Activity Drop Detection Query

SQL & DataModel answer
MediumModel answerSQL & Datasqlwindow-functionstime-series

Last updated

Write SQL to find countries where the most creators posted significantly less in the recent week than their baseline, accounting for local time zones.

You're analyzing user engagement across different regions. Given the schema below, write a single ANSI SQL query that identifies which countries have the highest proportion of content creators whose activity declined sharply in the most recent week compared to their baseline period. The query must respect each creator's local time zone when determining calendar day boundaries.

Schema:

creators
+------------+---------+----------------------+--------+
| creator_id | country | tz                   | status |
+------------+---------+----------------------+--------+
| 1          | US      | America/Los_Angeles  | active |
| 2          | US      | America/New_York     | active |
| 3          | JP      | Asia/Tokyo           | active |
| 4          | BR      | America/Sao_Paulo    | active |
| 5          | DE      | Europe/Berlin        | banned |
+------------+---------+----------------------+--------+

posts
+---------+------------+---------------------+---------+------------+
| post_id | creator_id | created_at_utc      | is_test | is_deleted |
+---------+------------+---------------------+---------+------------+
| 101     | 1          | 2025-08-26 23:30:00 | 0       | 0          |
| 102     | 1          | 2025-08-27 08:10:00 | 0       | 0          |
| 103     | 2          | 2025-08-29 01:05:00 | 1       | 0          |
| 104     | 2          | 2025-08-30 13:40:00 | 0       | 0          |
| 105     | 3          | 2025-08-20 09:00:00 | 0       | 1          |
| 106     | 3          | 2025-08-31 23:55:00 | 0       | 0          |
| 107     | 4          | 2025-08-05 16:30:00 | 0       | 0          |
| 108     | 4          | 2025-08-28 22:05:00 | 0       | 0          |
| 109     | 4          | 2025-09-01 01:10:00 | 0       | 0          |
| 110     | 1          | 2025-07-30 11:00:00 | 0       | 0          |
+---------+------------+---------------------+---------+------------+

Requirements:

Use 2025-09-01 as the reference date (do not use CURRENT_DATE).

Define two analysis windows based on each creator's local time zone:

  • Recent period: 2025-08-25 through 2025-08-31 (7 days)
  • Baseline period: 2025-07-28 through 2025-08-24 (28 days)

Calculate daily posting rates:

  • recent_rate = posts in recent period / 7
  • baseline_rate = posts in baseline period / 28

A creator has "declined activity" if:

  • Their recent_rate < 0.7 × baseline_rate (more than 30% drop)
  • They had at least 1 valid post in the baseline period

Only include creators with status = 'active' who posted at least once during the baseline. Exclude posts where is_test = 1 or is_deleted = 1.

Output columns:

  • country
  • total_active_creators (count of active creators with ≥1 baseline post)
  • declining_creators (count meeting decline criteria)
  • share_declining (decimal fraction)
  • median_gap_days (median time in days between consecutive posts during the recent period, calculated across all valid posts in that country)

Return the top 10 countries ordered by share_declining descending, then total_active_creators descending.

Assume your SQL engine supports time zone conversion functions like CONVERT_TIMEZONE or FROM_UTC_TIMESTAMP. Explain your approach to calculating the median gap and any edge cases you handle.

Hints

Hint 1

Convert UTC timestamps to local dates before applying date filters—a post at 23:55 UTC may fall on different calendar days depending on the creator's time zone.

Hint 2

To avoid floating-point division issues, rearrange the inequality: recent_rate < 0.7 × baseline_rate becomes 10 × recent_count < 7 × (baseline_count / 4), or more cleanly, 40 × recent_count < 7 × baseline_count.

Hint 3

Use LAG() within each creator's posts (ordered by timestamp) to compute inter-post intervals, then aggregate those intervals to a per-country median using PERCENTILE_CONT.