Skip to content
RED SAGSkills

Ledger Running Balance

ledger_entries holds credits (positive amount) and debits (negative amount) for each account. Several entries can share a date; within a date, entries apply in entry_id order.

For every entry, show the account's running balance after that entry, rounded with ROUND(x, 2).

Return columns account_id, entry_date, entry_id, running_balance. Order by account_id, then entry_date, then entry_id, all ascending.

Schema

CREATE TABLE ledger_entries (
  entry_id INTEGER PRIMARY KEY,
  account_id INTEGER NOT NULL,
  entry_date TEXT NOT NULL,
  amount REAL NOT NULL
);

Sample data

ledger_entries

entry_idaccount_identry_dateamount
11002026-01-011000
21002026-01-03-250
32002026-01-02500
41002026-01-05-100.5
52002026-01-04-600
62002026-01-06300
Hint 1

SUM(...) OVER (PARTITION BY account ORDER BY ...) gives a cumulative sum per account.

Hint 2

An entry_id can be smaller than an earlier-dated entry, so order the window by date first, then id.

Good practice for: Data Analyst, Backend Developer, Data Engineer, Database Administrator

SQL (SQLite)

Run checks your query on the sample data (Ctrl + '). Submit also checks hidden databases.