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
SQL Top Campaigns by Revenue
00:00
5 left

SQL Top Campaigns by Revenue

HardSQL · PostgreSQL

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
Interviewer

Your question is SQL Top Campaigns by Revenue. Start with the requirements and the three 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.