Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL ROI by Date and Channel

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

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

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
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results