Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Host Revenue Running Total
00:00
5 left

Host Revenue Running Total

MediumSQL · PostgreSQL

Problem

Write a query using window functions to calculate the running total of revenue generated per host, resetting the count at the start of each calendar year.

Use the hosts and trips tables. Include completed trips only. The running total must be chronological within each host and year, with trip_id as the tie-breaker for trips on the same date.

Output

  1. One row per completed trip with a matching host
  2. Columns: host_id, host_name, calendar_year, trip_id, trip_date, revenue, and running_total
  3. Sort by host_id, calendar_year, trip_date, and trip_id

Schema

hosts
ColumnTypeDescription
host_idPKINTUnique identifier for a host
host_nameVARCHAR(100)Display name of the host
trips
ColumnTypeDescription
trip_idPKINTUnique identifier for a trip
host_idINTHost associated with the trip
trip_dateDATEDate the trip was completed or recorded
revenueNUMERIC(10,2)Revenue attributed to the trip
statusVARCHAR(20)Trip processing status
Tableshoststrips
Interviewer

Your question is Host Revenue Running Total. 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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.