KatInterview

Monthly Country Dashboard: Signups, Conversion, Retention, GMV, Campaign

SQL & DataModel answer
MediumModel answerSQL & Datasqlcohort-analysisretention

Last updated

Write a single SQL query that builds a month-by-country report covering signups, 7-day conversion, day-7 retention, valid GMV, and last-touch top campaign, with dedup and UTC handling.

You are building a monthly growth report for a marketplace. You have three tables:

  • users(uid, created_at, country)
  • orders(order_id, uid, amount, created_at, status)
  • events(uid, ts, event_type, campaign)

Write one SQL query that returns one row per (month, country) with these columns:

  1. New users: users who signed up in that month, in that country.
  2. 7-day conversion rate: the share of those new users who placed a valid order within 7 days of signing up.
  3. 7-day retention: among user-days with activity on day D, the share that were also active on day D+7.
  4. GMV: the sum of order amounts, excluding refunded and canceled orders.
  5. Top campaign: the campaign that wins the most orders under last-touch attribution, using campaign-tagged events.

Your query must also:

  • Cope with late-arriving events.
  • Deduplicate events on (uid, ts, event_type).
  • Keep timezones consistent across all tables.
  • Explain your window-function choices (which function, partitioning, ordering, and why).

State your assumptions about metric definitions before the code. Mention where the definitions are ambiguous and how you resolved them.

Hints

Hint 1

Decide the grain and time anchor of each metric before writing SQL: signup month for new users and conversion, activity month for retention, order month for GMV and attribution.

Hint 2

Build one CTE per metric at (month, country) grain, then LEFT JOIN them all onto a spine of every (month, country) pair seen anywhere, so a country with revenue but no signups is not lost.

Hint 3

For retention, collapse events to distinct (uid, day) pairs and self-join on day + 7. For attribution, rank each order's prior tagged events by timestamp descending with ROW_NUMBER and keep rank 1, then rank campaigns per (month, country) the same way.