Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Ranking Campaigns by Revenue

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

Your question is Ranking Campaigns by Revenue. 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

Corsair's marketing team wants to compare campaign revenue across regions for campaigns active during the first half of 2025. Write a PostgreSQL query that ranks every campaign within its region, including campaigns with no qualifying revenue.

Requirements

  1. Aggregate revenue from revenue_events for dates from 2025-01-01 through 2025-06-30.
  2. Preserve campaigns with no matching revenue and report their revenue as 0.
  3. Rank campaigns within each region by revenue descending using RANK(). Ties must receive the same rank.
  4. Return results ordered by region, rank, and campaign ID.

Schema

campaigns
ColumnTypeDescription
campaign_idPKINTUnique campaign identifier
campaign_nameVARCHAR(100)Campaign name
regionVARCHAR(50)Target geographic region
revenue_events
ColumnTypeDescription
event_idPKINTUnique revenue event identifier
campaign_idINTLogical reference to a campaign; source data may contain unmatched IDs
event_dateDATEDate used for revenue attribution
revenueNUMERIC(12,2)Revenue attributed to the event
Tablescampaignsrevenue_events
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results