Explain how to calculate running totals or cumulative sums using window functions.

Quick Overview

Asks how to compute a running total or cumulative sum in SQL with window functions, including restarting it per group. Tests understanding of PARTITION BY, ORDER BY, the default window frame, and how ROWS and RANGE treat tied values.

Explain how to calculate running totals or cumulative sums using window functions.

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Explain how to calculate running totals or cumulative sums using SQL window functions. A typical setting is a `transactions` table with one row per transaction, where you want each row to show the total so far for its account. ```hint Watch the frame Look at what frame a window gets by default once it has an `ORDER BY`, and what happens to two rows that share the same sort value. ``` ### What a Strong Answer Covers - The window expression that produces a running total, and how `PARTITION BY` restarts it per group - The default frame when `ORDER BY` is present, and how `RANGE` and `ROWS` differ when sort values tie - How to make the result deterministic when the ordering column has duplicates - Practical details such as `NULL` values and running totals over already aggregated data ### Follow-up Questions - How would you compute a 7-day rolling sum instead of an all-time running total, and why does the choice between `ROWS` and `RANGE` matter there? - How would you produce a running total of daily revenue from a table of individual orders in a single query? - How would you write a running total in a database that has no window functions, and what does it cost? - How would you return only the first row per account where the running total crosses 1,000?

Overview: Asks how to compute a running total or cumulative sum in SQL with window functions, including restarting it per group. Tests understanding of PARTITION BY, ORDER BY, the default window frame, and how ROWS and RANGE treat tied values.

Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Explain how to calculate running totals or cumulative sums using SQL window functions. A typical setting is a transactions table with one row per transaction, where you want each row to show the total so far for its account.

What a Strong Answer Covers Guidance

  • The window expression that produces a running total, and how PARTITION BY restarts it per group
  • The default frame when ORDER BY is present, and how RANGE and ROWS differ when sort values tie
  • How to make the result deterministic when the ordering column has duplicates
  • Practical details such as NULL values and running totals over already aggregated data

Follow-up Questions Guidance

  • How would you compute a 7-day rolling sum instead of an all-time running total, and why does the choice between ROWS and RANGE matter there?
  • How would you produce a running total of daily revenue from a table of individual orders in a single query?
  • How would you write a running total in a database that has no window functions, and what does it cost?
  • How would you return only the first row per account where the running total crosses 1,000?