cswork / libraryOpen practice workspace

Coding questions · Meta

Compute ads revenue by geography in SQL

This question evaluates SQL data manipulation and revenue attribution skills, testing the ability to aggregate and compute business metrics such as total revenue, impressions, clicks, CTR, and revenue per 1k impressions from ad impressions, clicks, and billing records.

You have ad delivery logs for a shop-ads system.

Tables

ad_impressions

  • impression_id STRING (PK)
  • ts TIMESTAMP (UTC)
  • user_id STRING
  • shop_id STRING
  • country STRING
  • region STRING
  • ad_slot STRING

ad_clicks

  • click_id STRING (PK)
  • impression_id STRING (FK → ad_impressions.impression_id)
  • ts TIMESTAMP (UTC)

ad_billing

  • impression_id STRING (FK → ad_impressions.impression_id)
  • bill_ts TIMESTAMP (UTC)
  • billing_model STRING
    • values: 'CPC', 'CPM'
  • revenue_usd NUMERIC
    • For CPC, revenue is recorded on the clicked impression; for CPM, revenue is recorded per impression.

Assume timestamps are UTC and you should use bill_ts as the source of truth for revenue timing.

Task

Write a SQL query to compute ads revenue by geography for the last 30 days:

  • Group by country and region.
  • Output columns:
    • country, region
    • total_revenue_usd
    • impressions
    • clicks
    • ctr = clicks / impressions
    • revenue_per_1k_impressions = 1000 * total_revenue_usd / impressions
  • Return only geographies with at least 100,000 impressions in the period.
  • Order by total_revenue_usd descending.

Compute ads revenue by geography (last 30 days)

You are given ad delivery logs for a shop-ads system.

Compute ads revenue by geography for the 30-day window from 2025-05-02 00:00:00 (inclusive) to 2025-06-01 00:00:00 (exclusive).

Requirements:

  • Use ad_billing.bill_ts as the source of truth for whether revenue (and the associated impression/click) is in the window.
  • Group by country and region (from ad_impressions).
  • Output columns:
    • country, region
    • total_revenue_usd
    • impressions
    • clicks
    • ctr = clicks / impressions
    • revenue_per_1k_impressions = 1000 * total_revenue_usd / impressions
  • Return only geographies with at least 100000 impressions in the window.
  • Order by total_revenue_usd descending.

Notes:

  • ad_billing.revenue_usd is already recorded at the impression level: for CPC it is only recorded on clicked impressions, and for CPM it is recorded per impression.
  • Use safe division for rate calculations (avoid divide-by-zero).

Table ad_impressions

columntypeconstraints
impression_idVARCHAR(50)PRIMARY KEY, NOT NULL
tsTIMESTAMPNOT NULL
user_idVARCHAR(50)NOT NULL
shop_idVARCHAR(50)NOT NULL
countryVARCHAR(2)NOT NULL
regionVARCHAR(20)NOT NULL
ad_slotVARCHAR(30)NOT NULL

Sample data:

ad_slotcountryimpression_idregionshop_idtsuser_id
search_topUSimp_001CAs12025-05-10 12:00:00u1
search_topUSimp_002CAs22025-05-11 09:15:00u2
browseUSimp_003NYs32025-05-20 18:30:00u3
search_bottomCAimp_004ONs42025-05-25 07:45:00u4
browseUSimp_005CAs22025-05-28 21:05:00u5
search_topUSimp_006CAs12025-04-15 10:00:00u6

Table ad_clicks

columntypeconstraints
click_idVARCHAR(50)PRIMARY KEY, NOT NULL
impression_idVARCHAR(50)NOT NULL
tsTIMESTAMPNOT NULL

Sample data:

click_idimpression_idts
clk_100imp_0022025-05-11 09:16:00
clk_101imp_0042025-05-25 07:46:30
clk_102imp_0062025-04-15 10:01:00

Table ad_billing

columntypeconstraints
impression_idVARCHAR(50)NOT NULL
bill_tsTIMESTAMPNOT NULL
billing_modelVARCHAR(3)NOT NULL
revenue_usdDECIMAL(10,2)NOT NULL

Sample data:

bill_tsbilling_modelimpression_idrevenue_usd
2025-05-10 12:00:05CPMimp_0010.5
2025-05-11 09:16:10CPCimp_0020.8
2025-05-20 18:30:05CPMimp_0030.4
2025-05-25 07:46:40CPCimp_0041.2
2025-05-28 21:05:10CPCimp_0050
2025-04-15 10:00:10CPMimp_0060.55

Expected output

One row per (country, region) for billed impressions in the window [2025-05-02, 2025-06-01), including revenue and performance metrics. Because the sample data does not contain any geography with at least 100000 impressions in the window, the filtered result set is empty.

Hints

  1. Filter the population using ad_billing.bill_ts (not impression ts) for the date window.
  2. Join billing -> impressions to get geography, and left join clicks to count clicks.
  3. Use COUNT(DISTINCT ...) to avoid double counting and NULLIF to prevent divide-by-zero.