Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Running Total Top Three Days
00:00
5 left

Running Total Top Three Days

MediumSQL · PostgreSQL

Problem

Write a query using SQL window functions to calculate the running total revenue per merchant and identify their top 3 highest-grossing days.

Use completed transactions only. Treat NULL revenue as zero when calculating daily revenue, and aggregate multiple transactions for the same merchant and date.

Output

  1. One row per merchant-day included in that merchant's top three days.
  2. Columns: merchant_name, sale_date, daily_revenue, running_total, and top_day_rank.
  3. Rank days by daily revenue descending, breaking ties by date ascending. Order the final result by merchant name and rank.

Schema

merchants
ColumnTypeDescription
merchant_idPKINTUnique merchant identifier
merchant_nameVARCHAR(100)Merchant display name
merchant_transactions
ColumnTypeDescription
transaction_idPKINTUnique transaction identifier
merchant_idINTMerchant receiving the transaction
sale_dateDATECalendar date of the transaction
revenueDECIMAL(12,2)Revenue generated by the transaction
statusVARCHAR(20)Transaction processing status
Tablesmerchantsmerchant_transactions
Interviewer

Your question is Running Total Top Three Days. 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.