KatInterview

Compute Daily Conversion and User Order History with Deduplication

SQL & DataModel answer
MediumModel answerSQL & Datawindow-functionsaggregationdeduplication

Last updated

Write SQL to calculate daily conversion metrics and per-user order summaries over a week, handling event deduplication and missing days.

You are analyzing user behavior for an e-commerce platform. All timestamps are stored in UTC. For this exercise, assume "today" is 2025-09-01, and you need to analyze the past seven days: the inclusive range from 2025-08-26 00:00:00 through 2025-09-01 23:59:59.

You have three tables:

users

  • user_id (INT, primary key)
  • signup_at (TIMESTAMP)
  • tz (STRING, user's timezone)

orders

  • order_id (INT, primary key)
  • user_id (INT, foreign key)
  • order_ts (TIMESTAMP)
  • amount (DECIMAL(10,2))
  • status (STRING, either 'completed' or 'canceled')

events

  • event_id (INT, primary key)
  • user_id (INT, foreign key)
  • event_ts (TIMESTAMP)
  • event_type (STRING, e.g., 'view_product', 'add_to_cart', 'purchase')
  • product_id (INT)
  • device_id (STRING)

Sample data is provided below:

users

user_id | signup_at           | tz
1       | 2025-08-10 09:00:00 | America/Los_Angeles
2       | 2025-08-28 14:00:00 | UTC
3       | 2025-08-30 23:30:00 | America/New_York

orders

order_id | user_id | order_ts            | amount | status
101      | 1       | 2025-08-27 23:55:00 | 20.00  | completed
102      | 1       | 2025-08-28 00:05:00 | 15.00  | canceled
103      | 2       | 2025-08-31 12:00:00 | 9.99   | completed
104      | 2       | 2025-09-01 00:00:10 | 49.00  | completed
105      | 3       | 2025-08-26 23:59:59 | 5.00   | completed

events

event_id | user_id | event_ts            | event_type   | product_id | device_id
1        | 1       | 2025-08-27 23:50:00 | view_product | 555        | A
2        | 1       | 2025-08-27 23:50:00 | view_product | 555        | A
3        | 1       | 2025-08-28 00:03:00 | add_to_cart  | 555        | A
4        | 1       | 2025-08-28 00:04:00 | purchase     | 555        | A
5        | 2       | 2025-08-31 11:58:00 | view_product | 777        | B
6        | 3       | 2025-08-26 23:55:00 | view_product | 999        | C
7        | 3       | 2025-08-26 23:58:00 | purchase     | 999        | C
8        | 2       | 2025-09-01 00:00:10 | purchase     | 777        | B

Notice that events 1 and 2 are exact duplicates.

Your tasks:

Write Standard SQL (CTEs are allowed) to produce two result sets:

  1. Daily conversion metrics: For each day in the seven-day window, calculate:

    • unique_viewers: count of distinct users who had at least one view_product event that day (after deduplicating exact duplicate events)
    • purchasers: count of completed orders placed that day (exclude canceled orders)
    • conversion_rate: purchasers / unique_viewers (handle division by zero gracefully)

    Include all seven days in the output, even if some days have zero activity. Order by day ascending.

  2. User-level order summary: For each user in the users table, compute:

    • first_completed_order_ts: timestamp of their earliest completed order in the window (NULL if none)
    • last_completed_order_ts: timestamp of their latest completed order in the window (NULL if none)
    • total_completed_orders: count of completed orders in the window

    Use window functions rather than correlated subqueries. Order by user_id ascending.

Deduplication rule: Treat rows in events as duplicates if they share identical values for (user_id, event_ts, event_type, product_id, device_id). Keep only one representative row per duplicate group.

Notes:

  • Only completed orders count as purchases; ignore canceled orders entirely
  • All date calculations use UTC day boundaries
  • Since SQL returns one result set per query, you may present both queries sharing the same CTE block, or describe how you'd run them sequentially

Bonus: Sketch a Pandas solution for the daily metrics and deduplication logic.

Hints

Hint 1

Generate a date spine for all seven days to ensure days with zero activity still appear in the output.

Hint 2

Deduplicate events using ROW_NUMBER partitioned by the full duplicate key before performing any aggregations.

Hint 3

When joining users to orders, filter orders in the CTE or ON clause—not in WHERE—to preserve users with no completed orders.