Analyze Thirty-Day Ad Performance with SQL
MetaData Scientist · OnsiteLast updated
This problem tests proficiency in window functions, time-based aggregation, and join logic to analyze multi-dimensional advertising performance metrics.
Problem Statement
You are provided with two tables: ad_impressions and ad_conversions.
ad_impressions:
impression_id(int, primary key)ad_id(int)campaign_id(int)ad_type(varchar: 'video', 'display', 'search')impression_time(timestamp)user_id(int)
ad_conversions:
conversion_id(int, primary key)impression_id(int, foreign key)conversion_value(decimal)conversion_time(timestamp)
Tasks
- Identify the top 3
ad_idsby total impressions in the last 30 days. - Calculate the Click-Through-Rate (CTR) per
ad_typefor the last 30 days. Note: CTR is defined ascount(conversions) / count(impressions). - Calculate the average
conversion_valueper unique user for eachcampaign_id. - Find the hour of the day (0-23) that sees the highest number of total impressions across all ads.
Constraints
- Assume current time is 2023-10-31 23:59:59.
impression_timeis in UTC.
Example
If an ad appears 100 times and results in 5 conversions, the CTR is 0.05.
-- Your SQL query should return a result set with the appropriate columns for each task.
Hints
Hint 1
Use `CURRENT_DATE - INTERVAL '30 days'` to filter the impression data consistently.
Hint 2
For CTR, perform a `LEFT JOIN` on `impression_id` to ensure you count all impressions even if they have zero conversions.
Hint 3
When aggregating by hour, use `EXTRACT(HOUR FROM impression_time)` to group records regardless of the date.
Follow-up questions
- How would you handle timezones if the users were distributed globally and you wanted to measure 'local' peak hours?
- If the conversion attribution window was limited to 7 days post-impression, how would you modify the JOIN condition?
- What potential bias might occur if you only calculate average conversion value per conversion, rather than per unique exposed user?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.