Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Top Campaigns by Revenue

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

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

Collaborative Real Estate wants to identify its strongest marketing campaigns in Q1 2025. Write a PostgreSQL query that attributes won deal revenue to campaigns through leads and ranks campaigns within each marketing channel.

Requirements

  1. Include every campaign, including campaigns with no won revenue.
  2. Calculate Q1 2025 revenue and the previous-quarter revenue for each campaign.
  3. Rank campaigns within each channel by Q1 revenue, using campaign ID as a deterministic tie-breaker.
  4. Return only the top two campaigns per channel.
  5. Include each campaign's percentage change from the previous quarter, share of channel revenue, and revenue gap to the next-ranked campaign.

Schema

campaigns
ColumnTypeDescription
campaign_idPKINTUnique campaign identifier
campaign_nameVARCHAR(150)Collaborative Real Estate campaign name
channelVARCHAR(50)Marketing acquisition channel
statusVARCHAR(20)Current campaign status
leads
ColumnTypeDescription
lead_idPKINTUnique lead identifier
campaign_idINTCampaign that generated the lead
created_atDATEDate the lead was created
deals
ColumnTypeDescription
deal_idPKINTUnique deal identifier
lead_idINTLead associated with the deal
closed_atDATEDate the deal closed
revenueNUMERIC(12,2)Revenue attributed to the deal
deal_statusVARCHAR(20)Deal outcome
Tablescampaignsleadsdeals
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results