Quick Overview

This question evaluates proficiency in SQL data manipulation, specifically deduplication of event-level clicks by ad_request_id, time-window filtering, join-based enrichment with campaign metadata, and calculation of advertising metrics such as CTR and RPM.

Compute CTR, RPM, and daily RPM variability in SQL

Company: Roblox

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Technical Screen

Write ANSI-Postgres SQL to compute, for each campaign, over the last 7 days inclusive (assume "today" is 2025-09-01, so the window is 2025-08-26 through 2025-09-01): 1) Total impressions and total charged clicks (dedupe rule below), 2) CTR = charged_clicks / impressions, 3) RPM = (sum of charged click revenue in USD) / impressions * 1000, 4) Standard deviation of daily RPM across the 7 days (use stddev_samp on daily RPM), 5) Return only campaigns with at least 10,000 impressions in the 7-day window, 6) Order by RPM DESC, then by daily RPM stddev ASC. Important deduping: treat multiple clicks with the same ad_request_id as a single charged click worth the earliest click’s revenue; impressions remain one per ad_request_id. Schema and sample data (ASCII, minimal rows just to illustrate): Table: campaigns(campaign_id INT, campaign_name TEXT, vertical TEXT) +-------------+---------------+----------+ | campaign_id | campaign_name | vertical | +-------------+---------------+----------+ | 10 | "Shoes_US" | "Retail" | | 11 | "Games_UK" | "Gaming" | +-------------+---------------+----------+ Table: ad_events(dt DATE, event_time TIMESTAMP, ad_request_id TEXT, user_id INT, campaign_id INT, event_type TEXT, revenue_cents INT) +------------+---------------------+--------------+---------+-------------+------------+---------------+ | dt | event_time | ad_request_id| user_id | campaign_id | event_type | revenue_cents | +------------+---------------------+--------------+---------+-------------+------------+---------------+ | 2025-08-30 | 2025-08-30 10:00:01 | R1 | 101 | 10 | impression | 0 | | 2025-08-30 | 2025-08-30 10:00:03 | R1 | 101 | 10 | click | 120 | | 2025-08-30 | 2025-08-30 10:05:00 | R2 | 102 | 10 | impression | 0 | | 2025-08-30 | 2025-08-30 10:05:20 | R2 | 102 | 10 | click | 120 | | 2025-08-31 | 2025-08-31 11:01:00 | R3 | 103 | 11 | impression | 0 | | 2025-08-31 | 2025-08-31 11:01:10 | R3 | 103 | 11 | click | 80 | | 2025-08-31 | 2025-08-31 11:01:12 | R3 | 103 | 11 | click | 80 | | 2025-09-01 | 2025-09-01 09:00:00 | R4 | 104 | 10 | impression | 0 | +------------+---------------------+--------------+---------+-------------+------------+---------------+ Include all necessary CTEs (e.g., to pick the earliest click per ad_request_id), ensure no double-counting across days, and make your final SELECT exactly match the requirements above.

Overview: This question evaluates proficiency in SQL data manipulation, specifically deduplication of event-level clicks by ad_request_id, time-window filtering, join-based enrichment with campaign metadata, and calculation of advertising metrics such as CTR and RPM.

Write ANSI-Postgres SQL to compute, for each campaign, over the 7-day window FROM 2025-05-26 TO 2025-06-01 (inclusive): 1) Total impressions and total charged clicks (with the dedupe rule below), 2) CTR = charged_clicks / impressions, 3) RPM = (sum of charged click revenue in USD) / impressions * 1000, 4) Standard deviation of daily RPM across the days in the window (use stddev_samp on daily RPM), 5) Return only campaigns with at least 3 impressions in the 7-day window, 6) Order by RPM DESC, then by daily RPM stddev ASC. Important deduping rule: treat multiple clicks with the same ad_request_id as a single charged click worth the earliest click’s revenue; impressions remain one per ad_request_id. Use the schema and sample data below. Include all necessary CTEs (e.g., to pick the earliest click per ad_request_id), ensure no double-counting across days, and make your final SELECT exactly match the requirements above.

Tables

campaigns(campaign_id INT, campaign_name TEXT, vertical TEXT)

ad_events(dt DATE, event_time TIMESTAMP, ad_request_id TEXT, user_id INT, campaign_id INT, event_type TEXT, revenue_cents INT)

Hints

  1. First deduplicate clicks per ad_request_id using a window function (ROW_NUMBER) so only the earliest click is counted and its revenue is used.
  2. Aggregate at the (campaign_id, dt) level to compute daily impressions and daily RPM, then use stddev_samp over those daily RPM values per campaign.
RobloxData ScientistmediumData Manipulation (SQL/Python)

Compute CTR, RPM, and daily RPM variability in SQL

Write ANSI-Postgres SQL to compute, for each campaign, over the 7-day window FROM 2025-05-26 TO 2025-06-01 (inclusive):

  1. Total impressions and total charged clicks (with the dedupe rule below),
  2. CTR = charged_clicks / impressions,
  3. RPM = (sum of charged click revenue in USD) / impressions * 1000,
  4. Standard deviation of daily RPM across the days in the window (use stddev_samp on daily RPM),
  5. Return only campaigns with at least 3 impressions in the 7-day window,
  6. Order by RPM DESC, then by daily RPM stddev ASC.

Important deduping rule: treat multiple clicks with the same ad_request_id as a single charged click worth the earliest click’s revenue; impressions remain one per ad_request_id.

Use the schema and sample data below. Include all necessary CTEs (e.g., to pick the earliest click per ad_request_id), ensure no double-counting across days, and make your final SELECT exactly match the requirements above.

Expected Output

One row per campaign that has at least 3 impressions between 2025-05-26 and 2025-06-01 (inclusive). For each campaign, show total impressions, total charged clicks after click deduping per ad_request_id, CTR, RPM, and the sample standard deviation of daily RPM over the days where the campaign had impressions.

Tables

campaignsad_events
campaigns
ColumnType
campaign_idINT
campaign_nameTEXT
verticalTEXT
ad_events
ColumnType
dtDATE
event_timeTIMESTAMP
ad_request_idTEXT
user_idINT
campaign_idINT
event_typeTEXT
revenue_centsINT

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions