Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

CTR by Region and Device

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is CTR by Region and Device. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Yelp Ads needs a performance report for advertiser 101. Write a PostgreSQL query that calculates click-through rates for impressions served during June 2024, segmented by geographic region and device type.

Requirements

  1. LEFT JOIN the impression and click logs so impressions without clicks remain in the report.
  2. Return one row per region and device type, including NULL dimensions as Unknown.
  3. Calculate total impressions, distinct clicked impressions, and CTR as a percentage rounded to two decimals. Avoid counting multiple click events for one impression more than once.
  4. Sort by region and device type, with NULL source dimensions last.

Schema

ad_impressions
ColumnTypeDescription
impression_idPKINTUnique impression identifier
advertiser_idINTYelp Ads advertiser identifier
regionVARCHAR(50)Geographic region where the impression occurred
device_typeVARCHAR(20)Device used to view the advertisement
impression_tsTIMESTAMPTimestamp when the ad impression occurred
ad_clicks
ColumnTypeDescription
click_idPKINTUnique click event identifier
impression_idINTImpression associated with the click
clicked_atTIMESTAMPTimestamp when the click occurred
Tablesad_impressionsad_clicks
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results