Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top City-Pair Revenue by Weekday

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

Your question is Top City-Pair Revenue by Weekday. 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

RideNow wants to understand which origin-destination city pairs generate the most revenue on each day of the week. Write a SQL query to return the top 3 city pairs by total revenue for each weekday.

Requirements

  1. Join trip records to the cities table twice to get the origin and destination city names.
  2. Exclude trips that are not completed and exclude rows where either city is missing.
  3. Aggregate total revenue by day of week and city pair.
  4. Rank city pairs within each weekday by revenue in descending order and return only the top 3 per weekday.
  5. Output the weekday name, weekday number, city pair, total revenue, and rank. Order results by weekday number, then rank, then city pair.

Schema

trips
ColumnTypeDescription
trip_idPKINTUnique trip identifier
trip_dateDATEDate the trip occurred
origin_city_idINTOrigin city ID referencing cities.city_id
destination_city_idINTDestination city ID referencing cities.city_id
fare_amountDECIMAL(10,2)Base fare amount
surge_amountDECIMAL(10,2)Surge amount added to the fare
trip_statusVARCHAR(20)Trip lifecycle status
cities
ColumnTypeDescription
city_idPKINTUnique city identifier
city_nameVARCHAR(100)City name
regionVARCHAR(50)Operating region for the city
Tablestripscities
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results