Search RU
Tasks / SQL / Running total of payments
Statement medium

Running total of payments

Return every payment with a running total: `running_total` is the sum of
`amount` for the current payment and all earlier ones. When several payments
share the same date, `id` defines their order.

Result columns: `paid_at`, `amount`, `running_total`.

Sort by `paid_at`, then by `id` ascending.

SQL · PostgreSQL
sqlwindow-functionsrunning-totalsum-over
Schema
CREATE TABLE payments (
    id integer PRIMARY KEY,
    paid_at date NOT NULL,
    amount integer NOT NULL
);
Data
INSERT INTO payments (id, paid_at, amount) VALUES
    (1, '2026-04-01', 1000),
    (2, '2026-04-02', 2500),
    (3, '2026-04-02', 500),
    (4, '2026-04-05', 1200),
    (5, '2026-04-10', 800);
query.sql PostgreSQL 17
AI mentor

The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.

Log in and ask
Hints 1 of 3 unlocked · attempts: 0
Hint 1

Every row stays in the result, but also gets the sum of the rows before it.
That calls for a window, not `GROUP BY`.

Hint 2 — unlocks after 1 attempt
Hint 3 — unlocks after 3 attempts