Net balance by account

conditional logic

Net balance by account

Stripe SQL Interview Question

Stripe's account dashboard shows how much a business charged its customers and what is left in its balance after refunds and payouts.

For each account_id, return the total of charges as gross_charges and the total of every transaction as net_balance, both rounded to 2 decimal places. Refunds and payouts are already stored as negative amounts. Sort by account_id.

Asked of

  • Data Analyst
  • Business Analyst
  • BI Analyst
  • Product Analyst
  • Analytics Engineer

transactionsTable12 rows

Column NameType
txn_idBIGINT
account_idBIGINT
created_dateDATE
typeVARCHAR
amountDOUBLE

transactionsExample Input

txn_idaccount_idcreated_datetypeamount
190012024-08-01charge120
290022024-08-01charge75.5
390012024-08-02charge60.25
490012024-08-03refund-20
590022024-08-03charge210
690012024-08-05payout-150
790032024-08-05charge40
890022024-08-06refund-75.5
990032024-08-06charge55.75
1090022024-08-08payout-180
1190012024-08-09charge99.99
1290032024-08-10payout-60

Example Output

account_idgross_chargesnet_balance
9001280.24110.24
9002285.530
900395.7535.75

Explanation

Account 9001 charged 120.00, 60.25 and 99.99, a gross of 280.24. After a 20.00 refund and a 150.00 payout, 110.24 is left in its balance.

The example above is a small slice of the data. Your query runs against the full tables.