Interview › Running total
SQL interview: running total with SUM() OVER
A running total keeps every row. GROUP BY would collapse them. That is the whole trick.
Prompt. For each order, show the customer’s spend so far, ordered by order_date.
Hint: SUM(total_amount) OVER (PARTITION BY customer_id ORDER BY order_date, order_id)
SELECT order_id, customer_id, order_date, total_amount,
SUM(total_amount) OVER (
PARTITION BY customer_id
ORDER BY order_date, order_id
ROWS UNBOUNDED PRECEDING
) AS running_spend
FROM Orders;
ROWS UNBOUNDED PRECEDING is the honest default. RANGE can double-count same-day twins.
Then write it on a ticket
Monday is free. A colleague asks, you type, we mark it.
Clock in free