Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top 3 Customers by Merchant Category

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

Your question is Top 3 Customers by Merchant Category. 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

American Express wants to identify the highest-spending customers across merchant categories for recent portfolio analysis. Using transactions recorded through 2025-02-15, write a PostgreSQL query to find the top three customers in each merchant category during the 30-day period from 2025-01-16 through 2025-02-14.

Requirements

  1. Join transactions to customers and merchant categories.
  2. Filter transactions to the specified 30-day period.
  3. Calculate total spending for each customer within each merchant category.
  4. Use a window function to rank customers within each category, include all customers tied within the top three ranks, and sort the final result by category and spending.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique American Express customer identifier
customer_nameVARCHAR(100)Customer display name
merchant_categories
ColumnTypeDescription
category_idPKINTUnique merchant category identifier
category_nameVARCHAR(100)Merchant category name
transactions
ColumnTypeDescription
transaction_idPKINTUnique transaction identifier
customer_idINTCustomer associated with the transaction
category_idINTMerchant category associated with the transaction
transaction_dateDATEDate the transaction was recorded
amountNUMERIC(12,2)Transaction amount in US dollars
Tablescustomersmerchant_categoriestransactions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results