Interview Questions/SQL/Monthly Sales with Running Total

Monthly Sales with Running Total

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Description

You are given a table of sales records. Each record has an id, a sale date, and an amount. Write a SQL query to return for each calendar month: the total sales amount for that month (monthly_total), and the cumulative running total of all sales up to and including that month (running_total). Return the result ordered by month ascending. Table: Sales

Column NameTypeDescription
idINTPrimary key
sale_dateDATEDate of the sale
amountDECIMALSale amount

Database Schema (Inferred)

Sales

Column NameExample Value
id1
sale_date2023-01-10
amount500

Example

Sales

idsale_dateamount
12023-01-10500
22023-01-25300
32023-02-05700
42023-02-20200
52023-03-15900

Output

monthmonthly_totalrunning_total
2023-01800800
2023-029001700
2023-039002600

Explanation:

January has two sales totalling 800. February has two totalling 900; running total becomes 1700. March has 900; running total becomes 2600.

Approach hint

Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

SQL Editor
Loading...

Sales

idsale_dateamount
12023-01-10500
22023-01-25300
32023-02-05700
42023-02-20200
52023-03-15900

Output

monthmonthly_totalrunning_total
2023-01800800
2023-029001700
2023-039002600