Running revenue over time

window functions

Running revenue over time

Stripe SQL Interview Question

Stripe's dashboard shows each business a chart of its revenue building up over time. You are building the numbers behind that chart for one online store.

Using completed orders only, show each order's order_date, its amount, and the total revenue up to and including that order as running_total. Sort the rows by order_date, earliest first.

Asked of

  • Data Analyst
  • BI Analyst
  • Product Analyst
  • Analytics Engineer
  • Data Engineer

ordersTable35 rows

Column NameType
order_idBIGINT
customer_idBIGINT
order_dateDATE
statusVARCHAR
amountDOUBLE

ordersExample Input

order_idcustomer_idorder_datestatusamount
100112024-01-05completed120.5
100222024-01-07completed2400
100312024-01-11completed89.99
100432024-01-15canceled45
100542024-01-18completed610.75
100622024-01-22completed1850.25
100752024-01-29completed3200
100812024-02-02refunded75
100962024-02-06completed55.4
101032024-02-09completed132.6

Example Output

order_dateamountrunning_total
2024-01-05120.5120.5
2024-01-0724002520.5
2024-01-1189.992610.49
2024-01-18610.753221.24
2024-01-221850.255071.49
2024-01-2932008271.49
2024-02-0655.48326.89
2024-02-09132.68459.49

Explanation

The first completed order is worth 120.50, so the running total starts at 120.50. The next completed order adds 2,400, bringing it to 2,520.50. The canceled order on January 15 does not appear and does not change the total.

The example above is a small slice of the data. Your query runs against the full tables.