Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Top 3 Campaigns by Month

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

Your question is SQL Top 3 Campaigns by Month. 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

Arby’s Marketing Analytics team wants to identify the strongest campaigns each month. Write a PostgreSQL query that returns the top three campaigns by revenue for every calendar month.

Requirements

  1. Include only revenue events with event_status = 'completed'.
  2. Aggregate revenue by campaign and calendar month using event_date.
  3. Rank campaigns within each month and return no more than the top three, breaking revenue ties with the lowest campaign_id.
  4. Return the month, campaign ID, campaign name, total revenue, and monthly rank in chronological order.

Schema

campaigns
ColumnTypeDescription
campaign_idPKINTEGERUnique campaign identifier
campaign_nameVARCHAR(100)Arby’s campaign name
channelVARCHAR(30)Primary marketing channel
statusVARCHAR(20)Campaign lifecycle status
campaign_revenue
ColumnTypeDescription
revenue_event_idPKINTEGERUnique revenue event identifier
campaign_idINTEGERCampaign associated with the revenue event
event_dateDATEDate the revenue was attributed
revenueNUMERIC(12,2)Revenue attributed to the event
event_statusVARCHAR(20)Revenue event processing status
Tablescampaignscampaign_revenue
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results