Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
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 feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Ranking Campaigns by Revenue
00:00
5 left

Ranking Campaigns by Revenue

MediumSQL · PostgreSQL

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
Interviewer

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