KatInterview

Campaign Attribution: Define Metrics and Write SQL for Purchase Impact

SQL & DataModel answer
MediumModel answerSQL & Datasqlmetricsattribution

Last updated

Design attributable, time-bounded conversion metrics for a marketing campaign and implement the SQL to measure, baseline, and diagnose them across a five-table schema.

Context

Your team ran an outreach campaign (P_launch) contacting users via email, push, and SMS. You have five tables and need to (a) define the right metrics, (b) compute them in SQL, (c) build a fair comparison group, (d) diagnose a sudden drop, and (e) guard against a common bias.

Schema

users(user_id INT, signup_date DATE, country STRING)
events(user_id INT, event_time TIMESTAMP, event_name STRING, product_id INT, device STRING)
purchases(order_id INT, user_id INT, order_time TIMESTAMP, amount DECIMAL(10,2), product_id INT)
marketing_contacts(contact_id INT, user_id INT, contact_time TIMESTAMP, channel STRING, campaign STRING)
products(product_id INT, category STRING, launched_at DATE)
<details> <summary>Sample rows</summary>

users

user_idsignup_datecountry
12025-07-15US
22025-07-20US
32025-07-25CA
42025-08-01US
52025-08-05IN
62025-08-10US

events

user_idevent_timeevent_nameproduct_iddevice
12025-08-11 09:00:00page_view101iOS
12025-08-12 10:00:00add_to_cart101iOS
12025-08-15 12:00:00purchase101iOS
22025-08-18 14:00:00page_view102Web
22025-08-19 16:00:00purchase102Web
32025-08-20 11:30:00page_view101Android
42025-08-21 09:15:00page_view101iOS
42025-08-28 17:45:00purchase101iOS
52025-08-22 08:05:00unsubscribeNULLWeb
62025-08-23 19:20:00add_to_cart102Android

purchases

order_iduser_idorder_timeamountproduct_id
500112025-08-15 12:00:00199.99101
500222025-08-19 16:00:0049.99102
500342025-08-28 17:45:00129.00101
500462025-08-25 20:10:0059.00102

marketing_contacts

contact_iduser_idcontact_timechannelcampaign
900112025-08-11 08:00:00emailP_launch
900222025-08-18 09:00:00pushP_launch
900342025-08-21 09:00:00emailP_launch
900462025-08-23 09:00:00smsP_launch

products

product_idcategorylaunched_at
101Elec2025-07-01
102Apparel2025-08-01
</details>

Tasks

A — Metric definitions

Define a primary success metric that is attributable to a single exposure, bounded to a fixed post-contact window, and robust to a small number of heavy buyers inflating the result. Add two guardrail metrics: one that captures negative user experience quickly, and one that signals delivery or landing-page problems well before the primary metric moves. Write precise formulas for all three.

B — Weekly conversion and revenue by country

Write SQL to compute, for each calendar week of first contact and each country:

  • 14-day post-contact purchase conversion rate
  • Average revenue per contacted user over the same window

Rules: use only each user's first contact in the campaign; exclude any purchase that happened strictly before that contact; exclude cohorts whose 14-day window has not yet closed relative to a watermark parameter :as_of_ts.

C — Matched non-contacted baseline

Write SQL that pairs each contacted user with exactly one non-contacted user who signed up in the same calendar week and the same country. Break ties deterministically by picking the smallest user_id. Compute the 14-day purchase rate for the matched non-contacted users using the same contact-time anchor as their paired contacted user.

D — Root-cause breakdown for a conversion drop

On 2025-08-20, the 14-day conversion rate for US contacted users falls roughly 20 percentage points below its prior 7-day average. Write SQL that produces a breakdown table sliced by device and product_id for users contacted on 2025-08-20, showing:

  • count_contacted
  • conv_14d for that day
  • conv_14d_prior (average over the preceding 7 days, same slice)
  • delta (today minus prior)
  • approx_contribution (approximate share of the total drop: count_contacted × delta)

Return only the top 3 slices by absolute contribution to the drop.

E — Unsubscribe bias check

In one or two sentences, describe one SQL or pipeline check you would add to confirm that users who unsubscribed immediately after being contacted are not silently distorting your conversion numbers.


What is expected: written metric formulas for part A, working SQL for parts B–D, and a concise written answer for part E. Explain any non-obvious choices (join order, maturity filter, revenue source).

Hints

Hint 1

For part A, a binary per-user flag (converted: yes/no) is more robust to outliers than summing revenue directly as the primary metric — a handful of high-value orders can swing a ratio wildly. Revenue belongs in a secondary metric with users in the denominator, not orders.

Hint 2

For part B, aggregate purchases per user before joining to dimension tables like users. Joining first and then aggregating risks row multiplication if any join key is not unique, silently inflating both conversion counts and revenue.

Hint 3

For part D, contribution to the drop is proportional to both the size of the slice and how far that slice's rate moved. A slice with a large count and a moderate delta can matter more than a small slice with a dramatic delta — so rank by count × delta, not by delta alone.