Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Median CTR by Meta Campaign
00:00
5 left

Median CTR by Meta Campaign

HardSQL · PostgreSQL

Problem

Meta Ads analysts want to compare campaign performance using the median click-through rate (CTR) across daily delivery records. Write a PostgreSQL query to return the median CTR for each ad campaign using window functions.

CTR for a row is defined as clicks::numeric / impressions. Rows with impressions = 0 or impressions IS NULL should be excluded from the median calculation.

Requirements

  1. Compute CTR for each valid row in ad_delivery.
  2. For each campaign_id, calculate the median CTR using window functions rather than PERCENTILE_CONT.
  3. If a campaign has an odd number of valid CTR rows, return the middle CTR.
  4. If a campaign has an even number of valid CTR rows, return the average of the two middle CTR values.
  5. Output campaign_id, campaign_name, and median_ctr, ordered by campaign_id.

Schema

meta_campaigns
ColumnTypeDescription
campaign_idPKINTUnique campaign identifier
campaign_nameVARCHAR(100)Campaign name in Meta Ads Manager
objectiveVARCHAR(50)Campaign objective
ad_delivery
ColumnTypeDescription
delivery_idPKINTUnique delivery record
campaign_idINTCampaign identifier
delivery_dateDATEDaily delivery date
impressionsINTNumber of impressions served
clicksINTNumber of clicks received
Tablesmeta_campaignsad_delivery
Interviewer

Your question is Median CTR by Meta Campaign. Start with the requirements and the two tables in the Question tab.

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.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.