Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Top 3 Transactions Per Customer
00:00
5 left

Top 3 Transactions Per Customer

MediumSQL · PostgreSQL

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
Interviewer

Your question is Top 3 Transactions Per Customer. Start with the requirements and the three tables in the Question tab.

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.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.