Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Window Functions for MoM Change

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

Your question is Window Functions for MoM Change. Start with the requirements and the three 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

Capital One's card analytics team wants to monitor monthly spending volume per cardholder across its credit card portfolio. Write a PostgreSQL query that calculates the month-over-month change in the average monthly transaction volume.

Assume transaction volume means the sum of posted purchase amounts for each cardholder during a calendar month. Include only cardholders with at least one qualifying transaction in that month. Exclude pending transactions, refunds, and transactions with a NULL amount.

Requirements

  1. Join cardholders, cards, and transactions to associate each transaction with its cardholder.
  2. Aggregate transaction volume first at the cardholder-month level.
  3. Calculate the average cardholder volume for each month.
  4. Use LAG to calculate the month-over-month percentage change, returning NULL for the first month.
  5. Round average volume and percentage change to two decimal places and order results chronologically.

Schema

cardholders
ColumnTypeDescription
cardholder_idPKINTEGERUnique cardholder identifier
cardholder_nameVARCHAR(100)Cardholder's full name
segmentVARCHAR(30)Customer portfolio segment
cards
ColumnTypeDescription
card_idPKINTEGERUnique card identifier
cardholder_idINTEGERReferences cardholders.cardholder_id
card_statusVARCHAR(20)Current card status
transactions
ColumnTypeDescription
transaction_idPKINTEGERUnique transaction identifier
card_idINTEGERReferences cards.card_id
transaction_tsTIMESTAMPTransaction timestamp
transaction_typeVARCHAR(20)Purchase or refund
statusVARCHAR(20)Transaction processing status
amountNUMERIC(12,2)Transaction amount in dollars
Tablescardholderscardstransactions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results