Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Running Monthly Transaction Balance

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

Your question is Running Monthly Transaction Balance. Start with the requirements and the two tables 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

JPMorganChase wants a transaction-level view of customer credit card activity. Write a PostgreSQL query that calculates each user's running balance separately for every calendar month.

Requirements

  1. Join posted transactions to users and return the user's ID and name.
  2. Derive month_start as the first day of the transaction month.
  3. Calculate a cumulative balance partitioned by user and month, ordered by transaction date and transaction ID.
  4. Treat a NULL transaction amount as zero and exclude declined, pending, or NULL-status transactions.

Schema

users
ColumnTypeDescription
user_idPKINTEGERUnique customer identifier
full_nameVARCHAR(100)Customer's full name
credit_card_transactions
ColumnTypeDescription
transaction_idPKINTEGERUnique transaction identifier
user_idINTEGERReferences users.user_id
transaction_dateDATEDate the transaction occurred
amountNUMERIC(12,2)Transaction amount, where negative values represent credits or refunds
statusVARCHAR(20)Transaction processing status
Tablesuserscredit_card_transactions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results