KatInterview

Daily Spend Leaders and Cost-Efficiency by Campaign Goal

SQL & DataModel answer
MediumModel answerSQL & Datawindow-functionsaggregationjoins

Last updated

Rank the top daily advertisers with ties, then compute cost per KPI by goal using a launch-date filter and safe division.

You work on a team that tracks paid advertising performance. Three tables capture spend, conversion outcomes, and campaign metadata.

Table: campaign_spend

ColumnTypeDescription
dateDATEUTC calendar date.
advertiser_idBIGINTUnique advertiser identifier.
campaign_idBIGINTUnique campaign identifier.
creative_groupVARCHARSub-grouping within a campaign.
ad_spendDECIMALDollars spent on that row.
impressionsBIGINTImpressions served.

Table: campaign_conversion

ColumnTypeDescription
dateDATEUTC calendar date.
advertiser_idBIGINTUnique advertiser identifier.
campaign_idBIGINTUnique campaign identifier.
creative_groupVARCHARSub-grouping within a campaign.
installsBIGINTInstall count attributed to the campaign.
kpi_cntBIGINTFinal KPI conversion count for the campaign's optimization goal.

Table: campaign_goal

ColumnTypeDescription
campaign_idBIGINTUnique campaign identifier.
campaign_goalVARCHAROptimization goal (e.g., install, purchase, signup).
campaign_launch_dateDATEUTC date the campaign went live.

Assumptions:

  • Each campaign_id maps to exactly one campaign_goal.
  • Spend and conversion rows share the grain (date, advertiser_id, campaign_id, creative_group), but a row may exist in one table without a matching row in the other.

Write SQL to answer both of the following questions.

Part 1 — Top 3 advertisers by daily spend

Collapse spend to the (date, advertiser_id) level and rank advertisers within each day by total spend, highest first. Return every advertiser whose rank falls within the top 3, including all tied advertisers.

Required output columns: date, advertiser_id, daily_ad_spend, spend_rank.

Part 2 — Cost per KPI by campaign goal

For each campaign_goal, sum total ad spend and total KPI conversions across every campaign sharing that goal. Only include rows dated on or after the campaign's launch date. Divide total spend by total KPI count to produce cost_per_kpi; return NULL instead of dividing by zero when the KPI total is zero.

Required output columns: campaign_goal, total_ad_spend, total_kpi_cnt, cost_per_kpi.

Hints

Hint 1

Part 1: The source table has one row per campaign and creative, not per advertiser per day. Aggregate first, then apply your ranking function — you cannot rank raw rows. Think about which of ROW_NUMBER, RANK, and DENSE_RANK handles ties the way the problem requires.

Hint 2

Part 2: Spend and conversion data live in separate tables and may not have matching rows at every grain. A straight inner join will silently drop spend that has no corresponding conversion row, which understates cost. Consider stacking both tables with UNION ALL before joining to campaign_goal, or use a FULL OUTER JOIN on pre-aggregated CTEs.

Hint 3

For the launch-date filter in Part 2, apply it after joining to campaign_goal so each fact row is compared against its own campaign's launch date — not a fixed cutoff. For safe division, NULLIF(denominator, 0) turns a zero into NULL, making the whole expression NULL rather than throwing an error.