Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Local Campaigns Per Region

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

Your question is Top Local Campaigns Per Region. Start with the requirements and the three 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

Google Ads regional marketing teams need to compare local campaigns by return on ad spend, or ROAS. Write a PostgreSQL query that ranks eligible local campaigns within each region and preserves ties at the third rank.

Requirements

  1. Include only local campaigns with status = 'active' and metrics from January 2025.
  2. Aggregate spend, revenue, impressions, clicks, and conversions by campaign.
  3. Calculate ROAS as total revenue divided by total spend, excluding campaigns with zero or missing spend.
  4. Use a window function so tied campaigns receive the same rank, and return every campaign whose rank is at most 3.
  5. Order the results by region, performance rank, and campaign ID.

Schema

regions
ColumnTypeDescription
region_idPKINTUnique region identifier
region_nameVARCHAR(100)Google Ads reporting region
ad_campaigns
ColumnTypeDescription
campaign_idPKINTUnique campaign identifier
region_idINTRegion targeted by the campaign
campaign_nameVARCHAR(150)Campaign display name
is_localBOOLEANWhether the campaign targets a local market
statusVARCHAR(20)Campaign lifecycle status
campaign_daily_metrics
ColumnTypeDescription
metric_idPKINTUnique daily metric record
campaign_idINTMeasured campaign
metric_dateDATEDate of the recorded metrics
impressionsINTNumber of ad impressions
clicksINTNumber of ad clicks
conversionsINTAttributed conversions
spendNUMERIC(12,2)Advertising spend
revenueNUMERIC(12,2)Attributed revenue
Tablesregionsad_campaignscampaign_daily_metrics
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results