Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Month-Over-Month Campaign Comparison

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

Your question is SQL Month-Over-Month Campaign Comparison. 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

Targetbase needs a monthly performance view for campaign optimization across its activation channels. Write a PostgreSQL query that compares each active campaign's metrics from January through April 2025.

Requirements

  1. Generate one row per active campaign and calendar month, including months with no activity.
  2. Aggregate impressions, clicks, spend, conversions, and revenue from daily metrics.
  3. Join campaign and channel attributes, then use LAG to return prior-month revenue.
  4. Calculate month-over-month revenue percentage change. Return NULL when prior revenue is zero or unavailable.
  5. Return results in campaign and chronological month order.

Representative Data

Schema

campaigns
ColumnTypeDescription
campaign_idPKINTCampaign identifier
campaign_nameVARCHAR(150)Campaign name
channel_idINTReferences channels.channel_id
objectiveVARCHAR(80)Campaign objective
start_dateDATECampaign start date
end_dateDATECampaign end date
channels
ColumnTypeDescription
channel_idPKINTChannel identifier
channel_nameVARCHAR(80)Targetbase activation channel
platformVARCHAR(60)Delivery platform
campaign_daily_metrics
ColumnTypeDescription
metric_datePKDATEMetric date
campaign_idPKINTReferences campaigns.campaign_id
impressionsINTDelivered impressions
clicksINTRecorded clicks
spendNUMERIC(12,2)Daily spend
conversionsINTAttributed conversions
revenueNUMERIC(12,2)Attributed revenue
Tablescampaignschannelscampaign_daily_metrics
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results