← Problems14. Month-over-month revenue growthMediumSQL
00:00 / 20:00

Month-over-month revenue growth

Medium41% solve itAsked at Amazon, Walmart, PayPal

Return revenue per month and the percentage change against the previous month. The first month has no previous month, so its growth is null. Months with no orders must still appear with zero revenue.

Input schema

orders order_id bigint ordered_at timestamp amount numeric status string -- 'PAID' | 'REFUNDED' | 'CANCELLED'

Constraints

  • Only PAID orders count toward revenue
  • growth_pct rounded to two decimal places
  • Gap months between the first and last order month must be present
  • Output ordered by month ascending
  • When the previous month's revenue is 0, growth_pct is null — a percentage change from zero is undefined

Example 1growth against the previous month

Input · orders
amountstatusorder_idordered_at
10000PAID12024-01-15 00:00:00
12500PAID22024-02-10 00:00:00
Output
monthrevenuegrowth_pct
2024-0110000null
2024-021250025

Example 2only PAID orders count

Input · orders
amountstatusorder_idordered_at
100PAID12024-01-05 00:00:00
900REFUNDED22024-01-06 00:00:00
150PAID32024-02-06 00:00:00
500CANCELLED42024-02-07 00:00:00
Output
monthrevenuegrowth_pct
2024-01100null
2024-0215050

Topics

window functionsaggregation

Similar problems

Community-reported interview topic. Not an official company question and no affiliation is implied.

solution.sql
Loading editor…
Draft not saved yet · Spaces 4 · UTF-8⌘↵ run ⌘⇧↵ submit
Nothing run yet

Run against the public tests, or submit to score against all of them.