How would you calculate a running total of sales for each product?

SQL
Medium
87K views

This question tests knowledge of window functions for analytical calculations.

Why Interviewers Ask This

Running totals are common in reporting. Interviewers evaluate your ability to use analytic functions to accumulate data over time or partitions efficiently.

How to Answer This Question

Identify the need for SUM() OVER(). Specify PARTITION BY for grouping by product and ORDER BY for time sequence. Explain ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.

Key Points to Cover

  • Window functions
  • Partitioning logic
  • Ordering sequence
  • Accumulation method

Sample Answer

To calculate a running total, I use a window function like SUM(amount) OVER (PARTITION BY product_id ORDER BY sale_date). This accumulates the sum from the start up to the current row for each product, keeping row detail…

Common Mistakes to Avoid

  • Using GROUP BY incorrectly
  • Forgetting ORDER BY
  • Not partitioning properly

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 69 SQL questions