Derive Daily Closing Balances From a Transaction Ledger
Last updated: November 29, 2026
Quick Overview
Given an account opening-balance table and a posted transactions table, write SQL that returns every account's closing balance for every calendar day in a date range, including days with no activity.
Citi
November 29, 20260
0
0 solved
Given an account opening-balance table and a posted transactions table, write SQL that returns every account's closing balance for every calendar day in a date range, including days with no activity.
Citi's data engineering teams build balance and liquidity reporting for treasury clients and regulators, where a missing day is a reporting defect. The screen checks window functions, date spines and whether you notice that quiet days still need a balance.
What the Interviewer Expects
- Build a date spine and cross join with accounts
- Aggregate transactions to one net amount per account per day
- Use a running SUM window ordered by date
- Add the opening balance
- Explain why a LEFT JOIN plus COALESCE is needed
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you handle back-valued transactions that post today with an earlier value date?
- How would you make this incremental instead of recomputing history every night?
- What changes for multi-currency accounts reported in USD?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
accounts(account_id, opening_balance, opening_date) and transactions(txn_id, account_id, value_date, amount) where amount is signed (credits positive). Report range is 2026-01-01 to `2026-01-3...
Approach
- Generate one row per day in the range (a date spine).
- Cross join it with accounts so every account has every day.
- Sum transactions per account per day.
- Left join, coalesce missing days to ...