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 ROI by Date and Channel
00:00
5 left

SQL ROI by Date and Channel

HardSQL · PostgreSQL

Problem

Business context

Bolt marketing analysts need to compare revenue generated with campaign costs across cities and channels. The source tables contain city-level records, but the required KPI is reported at the date and channel level.

Task

Write a PostgreSQL query that calculates Bolt ROI by date and channel, where ROI is total revenue divided by total cost.

Requirements

  1. Aggregate each source to the date, channel, and city grain before joining to prevent revenue-cost fanout.
  2. Preserve valid date, channel, and city combinations appearing in either table.
  3. Treat missing revenue or cost as zero, and return NULL ROI when total cost is zero.
  4. Include the previous available date's ROI for each channel and rank channels by ROI for each date, with zero-cost channels ranked last.
  5. Return results ordered by date and channel.

Schema

city_cost
ColumnTypeDescription
dateDATEDate on which the channel cost was incurred
channelVARCHAR(40)Bolt marketing or acquisition channel
cityVARCHAR(80)Bolt operating city
costNUMERIC(12,2)Cost attributed to the city and channel
order_rev
ColumnTypeDescription
dateDATEDate on which the order was completed
channelVARCHAR(40)Bolt channel attributed to the order
cityVARCHAR(80)City associated with the order
order_idBIGINTUnique order identifier
revenueNUMERIC(12,2)Revenue attributed to the order
Tablescity_costorder_rev
Interviewer

Your question is SQL ROI by Date and Channel. 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.