Your question is Detect Row-by-Row Balance Changes. Start with the requirements and the one table on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
You are given account transactions and need to produce a row-by-row balance audit for each account. Write a PostgreSQL query that shows each transaction with the prior balance, the balance change from the previous row, and the running balance after the transaction. The logic must work across records in timestamp order, so this is a good test of how you handle row-dependent calculations.
| Column | Type | Description |
|---|---|---|
| transaction_idPK | INT | Primary key for the transaction |
| account_id | INT | Account identifier |
| transaction_ts | TIMESTAMP | Transaction timestamp used for sequencing |
| transaction_type | VARCHAR(20) | Transaction category such as deposit or withdrawal |
| amount | NUMERIC(12,2) | Signed amount applied to the account |
| note | TEXT | Optional note or reason for the transaction |