TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2842 736
SQL Interview Series โ€” Part 11

๐Ÿ“Œ Question 11: Calculate a Running Total of Sales

Suppose you have a Sales table:

sale_id | sale_date  | amount
--------|------------|-------
1 | 2026-01-01 | 1000
2 | 2026-01-02 | 1500
3 | 2026-01-03 | 800
4 | 2026-01-04 | 1200


โ“ Calculate the cumulative (running) sales amount for each day.

๐Ÿ’ก Approach

A running total means:

Current day's sales + all previous sales

For example:

1000

1000 + 1500 = 2500

2500 + 800 = 3300

3300 + 1200 = 4500

The easiest approach is to use the "SUM()" window function.

๐Ÿ“Œ SQL Solution

SELECT
sale_date,
amount,
SUM(amount) OVER (
ORDER BY sale_date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_total
FROM Sales
ORDER BY sale_date;


๐Ÿ”Ž How does it work?

1๏ธโƒฃ "ORDER BY sale_date"

Determines the order in which the running total is calculated.

2๏ธโƒฃ "SUM(amount) OVER(...)"

Calculates the sum without collapsing the rows like a normal "GROUP BY".

3๏ธโƒฃ "UNBOUNDED PRECEDING"

Means:

Start from the first row.

4๏ธโƒฃ "CURRENT ROW"

Means:

Include the current row in the total.

So the calculation becomes:

Date        Amount    Running Total
---------- -------- -------------
Jan 01 1000 1000
Jan 02 1500 2500
Jan 03 800 3300
Jan 04 1200 4500


โš ๏ธ Interview Trap

Don't use:

GROUP BY sale_date

for a running total.

"GROUP BY" collapses rows into groups.

A window function allows us to calculate an aggregate while keeping individual rows.

Also, if multiple rows can have the same "sale_date", be careful about the ordering. A unique column such as "sale_id" may need to be included:

SUM(amount) OVER (
ORDER BY sale_date, sale_id
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
)


๐ŸŽฏ Double Tap โค๏ธ For More
  • โค 6
More from @sqlanalyst
  1. Oct 16, 2026๐——๐—ฎ๐˜๐—ฎ ๐˜€๐—ฐ๐—ถ๐—ฒ๐—ป๐—ฐ๐—ฒ ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐—ช๐—ถ๐˜๐—ต ๐—”๐—œ๐Ÿ˜ Future Ready Data Scienโ€ฆ
  2. Oct 13, 2026SQL Interview Series โ€” Part 8 ๐Ÿ“Œ Question 8: Find Customers Who Have Never Placed an Orderโ€ฆ
  3. Oct 13, 2026๐—ง๐—ข๐—ฃ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ข๐—จ๐—ฅ๐—ฆ๐—˜๐—ฆ ๐—ง๐—ข ๐—•๐—ข๐—ข๐—ฆ๐—ง ๐—ฌ๐—ข๐—จ๐—ฅ ๐—ฆ๐—ž๐—œ๐—Ÿ๐—Ÿ๐—ฆ ๐—œ๐—ก ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ! Explorโ€ฆ
  4. Oct 12, 2026SQL Interview Series โ€” Part 6 ๐Ÿ“Œ Question 6: Find the Top 2 Highest-Paid Employees in Eachโ€ฆ
  5. Oct 12, 2026๐Ÿš€ ๐—ช๐—ต๐—ถ๐—ฐ๐—ต ๐—ง๐—ฒ๐—ฐ๐—ต ๐—ฆ๐—ธ๐—ถ๐—น๐—น ๐—ช๐—ถ๐—น๐—น ๐—ฌ๐—ผ๐˜‚ ๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ? Want to builโ€ฆ
  6. Oct 9, 2026SQL Interview Series โ€” Part 5 ๐Ÿ“Œ Question 5: Find Employees Who Earn More Than Their Managโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’