Find the top 3 merchants by one user's total spend over a date range

Quick Overview

Write a SQL query that finds the three merchants where one user spent the most within a fixed date range, returning each merchant's name and total spend. It tests joins, filtering on an exclusive date bound, grouped sums, and a top-N cut with a deterministic tiebreaker.

Find the top 3 merchants by one user's total spend over a date range

Company: Plaid

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Find the top 3 merchants by total spend for the user with `user_id = 1`, counting only transactions where `transaction_date >= '2024-01-01'` and `transaction_date < '2024-01-31'`. The upper bound is exclusive, so the window runs from January 1 through January 30, 2024, and transactions dated January 31 are not counted. Return each merchant's `merchant_name` and `total_spend`, the sum of `amount` over those transactions. ```hint Sort before you cut The three rows come from the full sorted list of merchants. Check what your query returns when the third and fourth merchants have the same total. ``` ### Input Table(s) `transactions` | Column | Type | Meaning | |---|---|---| | transaction_id | INT | Transaction identifier. Primary key. | | user_id | INT | User who made the transaction. | | merchant_id | INT | Merchant the money was spent at. Foreign key to `merchants.merchant_id`. | | amount | DECIMAL(10,2) | Amount spent. Always positive. | | transaction_date | DATE | Date of the transaction. | Keys: `transaction_id` is the primary key, and `merchant_id` references `merchants.merchant_id`. `merchants` | Column | Type | Meaning | |---|---|---| | merchant_id | INT | Merchant identifier. Primary key. | | merchant_name | VARCHAR(100) | Merchant name. No uniqueness constraint. | Key: `merchant_id` is the primary key. ### Output Contract | Column | Meaning | |---|---| | merchant_name | The merchant's name. | | total_spend | Sum of `amount` over user 1's transactions at that merchant inside the window. No rounding is needed, since `amount` already has 2 decimal places. | Order the rows by `total_spend` descending, then by `merchant_id` ascending, and return only the first 3 rows. `merchant_id` is used for ordering but is not an output column. If fewer than 3 merchants qualify, return all of them. ### Example `merchants` | merchant_id | merchant_name | |---|---| | 10 | Coffee Corner | | 20 | Book Barn | | 30 | Fuel Stop | | 40 | Grocer Hub | | 50 | Movie House | `transactions` | transaction_id | user_id | merchant_id | amount | transaction_date | |---|---|---|---|---| | 1 | 1 | 10 | 12.50 | 2024-01-03 | | 2 | 1 | 10 | 7.50 | 2024-01-15 | | 3 | 1 | 20 | 45.00 | 2024-01-10 | | 4 | 1 | 30 | 20.00 | 2024-01-30 | | 5 | 1 | 40 | 15.00 | 2024-01-01 | | 6 | 1 | 40 | 5.00 | 2024-01-20 | | 7 | 1 | 40 | 100.00 | 2024-01-31 | | 8 | 2 | 50 | 300.00 | 2024-01-12 | | 9 | 1 | 50 | 80.00 | 2023-12-31 | Expected output: | merchant_name | total_spend | |---|---| | Book Barn | 45.00 | | Coffee Corner | 20.00 | | Fuel Stop | 20.00 | Transaction 7 is dated January 31, transaction 8 belongs to user 2 and transaction 9 is from 2023, so none of them count. User 1's totals are 45.00 at Book Barn and 20.00 each at Coffee Corner (12.50 + 7.50), Fuel Stop and Grocer Hub (15.00 + 5.00). The three-way tie at 20.00 is broken by `merchant_id`, so merchants 10 and 30 make the cut and Grocer Hub (40) does not. ### Constraints and Clarifications - `transactions` has at most 10,000 rows and `merchants` has at most 1,000 rows. - `amount` is always positive, so it is never NULL. `transaction_date` is always a valid DATE. - At least one transaction exists for `user_id = 1` in the window, so the result has at least one row. - Every `merchant_id` in `transactions` has a matching row in `merchants`. - Ties on `total_spend` are broken by `merchant_id` ascending. The same rule decides which merchant is kept when the third and fourth totals are equal, and the result never has more than 3 rows. - Two merchants can share a `merchant_name`. They are still different merchants with separate totals.

Overview: Write a SQL query that finds the three merchants where one user spent the most within a fixed date range, returning each merchant's name and total spend. It tests joins, filtering on an exclusive date bound, grouped sums, and a top-N cut with a deterministic tiebreaker.

Plaid logo
Plaid
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Find the top 3 merchants by total spend for the user with user_id = 1, counting only transactions where transaction_date >= '2024-01-01' and transaction_date < '2024-01-31'. The upper bound is exclusive, so the window runs from January 1 through January 30, 2024, and transactions dated January 31 are not counted. Return each merchant's merchant_name and total_spend, the sum of amount over those transactions.

Input Table(s)

transactions

ColumnTypeMeaning
transaction_idINTTransaction identifier. Primary key.
user_idINTUser who made the transaction.
merchant_idINTMerchant the money was spent at. Foreign key to merchants.merchant_id.
amountDECIMAL(10,2)Amount spent. Always positive.
transaction_dateDATEDate of the transaction.

Keys: transaction_id is the primary key, and merchant_id references merchants.merchant_id.

merchants

ColumnTypeMeaning
merchant_idINTMerchant identifier. Primary key.
merchant_nameVARCHAR(100)Merchant name. No uniqueness constraint.

Key: merchant_id is the primary key.

Output Contract

ColumnMeaning
merchant_nameThe merchant's name.
total_spendSum of amount over user 1's transactions at that merchant inside the window. No rounding is needed, since amount already has 2 decimal places.

Order the rows by total_spend descending, then by merchant_id ascending, and return only the first 3 rows. merchant_id is used for ordering but is not an output column. If fewer than 3 merchants qualify, return all of them.

Example

merchants

merchant_idmerchant_name
10Coffee Corner
20Book Barn
30Fuel Stop
40Grocer Hub
50Movie House

transactions

transaction_iduser_idmerchant_idamounttransaction_date
111012.502024-01-03
21107.502024-01-15
312045.002024-01-10
413020.002024-01-30
514015.002024-01-01
61405.002024-01-20
7140100.002024-01-31
8250300.002024-01-12
915080.002023-12-31

Expected output:

merchant_nametotal_spend
Book Barn45.00
Coffee Corner20.00
Fuel Stop20.00

Transaction 7 is dated January 31, transaction 8 belongs to user 2 and transaction 9 is from 2023, so none of them count. User 1's totals are 45.00 at Book Barn and 20.00 each at Coffee Corner (12.50 + 7.50), Fuel Stop and Grocer Hub (15.00 + 5.00). The three-way tie at 20.00 is broken by merchant_id, so merchants 10 and 30 make the cut and Grocer Hub (40) does not.

Constraints and Clarifications

  • transactions has at most 10,000 rows and merchants has at most 1,000 rows.
  • amount is always positive, so it is never NULL. transaction_date is always a valid DATE.
  • At least one transaction exists for user_id = 1 in the window, so the result has at least one row.
  • Every merchant_id in transactions has a matching row in merchants .
  • Ties on total_spend are broken by merchant_id ascending. The same rule decides which merchant is kept when the third and fourth totals are equal, and the result never has more than 3 rows.
  • Two merchants can share a merchant_name . They are still different merchants with separate totals.