Ad Click-to-Purchase Rate

ASKED IN INTERVIEW
4pts
Google, Amazon

Problem Statement

In digital advertising, "Conversion Rate" is a key performance indicator. Google and Amazon often look at attribution windows—specifically, did a user buy the product within a week of seeing the ad?

Write an SQL query to calculate the percentage of unique ad clicks that resulted in a purchase within a 7-day window.

Rules:

  • A "Conversion" is defined as a purchase where purchase_date >= click_date AND purchase_date <= click_date + 7 days.
  • Use unique click_id as the denominator.
  • If a single click leads to multiple purchases in that window, it still only counts as ONE converted click.
  • Output columns: total_clicks, converted_clicks, conversion_rate.
  • Round conversion_rate to 2 decimal places.
  • Return the result in descending order of conversion_rate.

Table Schema:

  • AdClicks: click_id, user_id, ad_id, click_date (DATE).
  • Purchases: purchase_id, user_id, product_id, purchase_date (DATE), amount.
Tests your understanding of
Self Join, DateTime Arithmetic, Aggregation and Ratios

Input Tables

AdClicks
click_id(INTEGER)user_id(INTEGER)click_date(DATE)
11012026-01-01
21022026-01-02
31032026-01-05
41042026-01-10
51052026-01-15
61012026-01-20
Purchases
purchase_id(INTEGER)user_id(INTEGER)purchase_date(DATE)
5011012026-01-03
5021022026-01-15
5031032026-01-06
5041012026-01-22

Expected Output

total_clicks(INTEGER)converted_clicks(INTEGER)conversion_rate(DECIMAL)
6350

Tags

MediumASKED IN INTERVIEWSelf JoinDateTime ArithmeticAggregationRatios
20-25 min
45%

Hints