Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top 3 Transactions Per Customer

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

Your question is Top 3 Transactions Per Customer. 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

Citi's data science team needs a monthly view of each customer's highest-value posted card transactions. Write a PostgreSQL query using CTEs and a window function to rank transactions within each customer-month group.

Requirements

  1. Join customers, accounts, and transactions.
  2. Include only transactions with transaction_status = 'POSTED', a positive non-null amount, and an OPEN account.
  3. Derive the month from transaction_timestamp using PostgreSQL date functions.
  4. Return the top three transactions per customer per month, ranking by amount descending, then timestamp and transaction ID descending to break ties.
  5. Return customers with fewer than three qualifying transactions for a month, and order the final output by customer, month, and rank.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique Citi customer identifier
customer_nameVARCHAR(100)Customer display name
accounts
ColumnTypeDescription
account_idPKINTUnique account identifier
customer_idINTCustomer who owns the account
account_typeVARCHAR(30)Account product type
account_statusVARCHAR(20)Current account status
card_transactions
ColumnTypeDescription
transaction_idPKINTUnique card transaction identifier
account_idINTAccount charged by the transaction
transaction_timestampTIMESTAMPTimestamp when the transaction occurred
amountNUMERIC(12,2)Transaction amount in dollars
transaction_statusVARCHAR(20)Processing status of the transaction
merchant_categoryVARCHAR(50)Merchant category classification
Tablescustomersaccountscard_transactions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results