Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Detect Row-by-Row Balance Changes

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

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 need to log in / sign up to run or submit.

Problem

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.

Schema

account_transactions
ColumnTypeDescription
transaction_idPKINTPrimary key for the transaction
account_idINTAccount identifier
transaction_tsTIMESTAMPTransaction timestamp used for sequencing
transaction_typeVARCHAR(20)Transaction category such as deposit or withdrawal
amountNUMERIC(12,2)Signed amount applied to the account
noteTEXTOptional note or reason for the transaction
Tablesaccount_transactions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results