logo

Back to questions

Final Account Balance

Easy

Given a table of bank deposits and withdrawals, return the final balance for each account.

Assumption:

  • All the transactions performed for each account are present in the table; no transactions are missing.

Table:

Column NameType
transaction_idinteger
account_idinteger
transaction_typevarchar
amountdecimal

Example Input:

transaction_idaccount_idtransaction_typeamount
123101Deposit10.00
124101Deposit20.00
125101Withdrawal5.00
126201Deposit20.00
128201Withdrawal10.00

Example Output:

account_idfinal_balance
10125.00
20110.00

Explanation:

In total, $30.00 were deposited to account 101, and $5.00 were withdrawn. Therefore, the final balance will be $30.00-$5.00 = $25.00

PostgreSQL 14

Refer friends to get bonus content & cool prizes.