Top 3 Customers by Transaction Volume (Last 30 Days)

Quick Overview

A SQL question that sums transaction amounts per customer inside a fixed 30-day window and returns the 3 customers with the highest totals. It tests inclusive date boundaries, grouped aggregation, and a top-N ordering that stays deterministic when totals tie.

Top 3 Customers by Transaction Volume (Last 30 Days)

Company: Fiserv

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

You are given a `transactions` table. Find the top 3 customers by total transaction amount over the last 30 days relative to the reference date `2024-06-30`, inclusive. That window runs from `2024-06-01` to `2024-06-30`. Return `customer_id` and `total_volume`, ordered by `total_volume` descending with ties broken by `customer_id` ascending. If fewer than 3 customers had transactions in the window, return only those. ```hint Check the start date Count the days from your start date through `2024-06-30` with both ends included. You should get 30. ``` ### Input Table(s) Table `transactions`, one row per transaction. Primary key: `transaction_id`. | Column | Type | Meaning | |---|---|---| | transaction_id | INT | Unique transaction id | | customer_id | INT | Customer who made the transaction | | amount | DECIMAL(10,2) | Transaction amount, non-negative | | transaction_date | DATE | Date of the transaction | ### Output Contract - Columns, in order: `customer_id`, `total_volume`. - `total_volume` is the sum of `amount` over the customer's transactions dated from `2024-06-01` to `2024-06-30`, both included. It is an exact decimal with 2 places. Do not round it further. - Rows are ordered by `total_volume` descending, then `customer_id` ascending. - Return at most 3 rows. ### Example Input `transactions`: | transaction_id | customer_id | amount | transaction_date | |---|---|---|---| | 1 | 101 | 120.00 | 2024-06-01 | | 2 | 102 | 80.50 | 2024-06-10 | | 3 | 101 | 30.00 | 2024-06-15 | | 4 | 103 | 95.25 | 2024-06-20 | | 5 | 104 | 95.25 | 2024-06-30 | | 6 | 102 | 500.00 | 2024-05-31 | | 7 | 105 | 300.00 | 2024-07-01 | | 8 | 102 | 19.50 | 2024-06-25 | Expected output: | customer_id | total_volume | |---|---| | 101 | 150.00 | | 102 | 100.00 | | 103 | 95.25 | Row 6 (`2024-05-31`) and row 7 (`2024-07-01`) fall outside the window, so customer 102 totals 80.50 + 19.50 = 100.00 and customer 105 has nothing in range. Rows 1 and 5 sit exactly on the window's first and last day and both count. Customers 103 and 104 tie at 95.25 for third place, and 103 takes the slot because its id is smaller. ### Constraints and Clarifications - `transactions` has 1 to 100,000 rows. - `amount` is non-negative with up to 2 decimal places, and `transaction_date` is a valid DATE. No column is NULL. - Rows dated before `2024-06-01` or after `2024-06-30` do not count. That includes rows dated after the reference date. - A customer can have many transactions in the window. Sum all of them. - Customers with no transactions in the window do not appear in the output. - When customers tie on `total_volume`, the smaller `customer_id` ranks higher, so a tie at third place still yields exactly 3 rows.

Overview: A SQL question that sums transaction amounts per customer inside a fixed 30-day window and returns the 3 customers with the highest totals. It tests inclusive date boundaries, grouped aggregation, and a top-N ordering that stays deterministic when totals tie.

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

You are given a transactions table. Find the top 3 customers by total transaction amount over the last 30 days relative to the reference date 2024-06-30, inclusive. That window runs from 2024-06-01 to 2024-06-30. Return customer_id and total_volume, ordered by total_volume descending with ties broken by customer_id ascending. If fewer than 3 customers had transactions in the window, return only those.

Input Table(s)

Table transactions, one row per transaction. Primary key: transaction_id.

ColumnTypeMeaning
transaction_idINTUnique transaction id
customer_idINTCustomer who made the transaction
amountDECIMAL(10,2)Transaction amount, non-negative
transaction_dateDATEDate of the transaction

Output Contract

  • Columns, in order: customer_id , total_volume .
  • total_volume is the sum of amount over the customer's transactions dated from 2024-06-01 to 2024-06-30 , both included. It is an exact decimal with 2 places. Do not round it further.
  • Rows are ordered by total_volume descending, then customer_id ascending.
  • Return at most 3 rows.

Example

Input transactions:

transaction_idcustomer_idamounttransaction_date
1101120.002024-06-01
210280.502024-06-10
310130.002024-06-15
410395.252024-06-20
510495.252024-06-30
6102500.002024-05-31
7105300.002024-07-01
810219.502024-06-25

Expected output:

customer_idtotal_volume
101150.00
102100.00
10395.25

Row 6 (2024-05-31) and row 7 (2024-07-01) fall outside the window, so customer 102 totals 80.50 + 19.50 = 100.00 and customer 105 has nothing in range. Rows 1 and 5 sit exactly on the window's first and last day and both count. Customers 103 and 104 tie at 95.25 for third place, and 103 takes the slot because its id is smaller.

Constraints and Clarifications

  • transactions has 1 to 100,000 rows.
  • amount is non-negative with up to 2 decimal places, and transaction_date is a valid DATE. No column is NULL.
  • Rows dated before 2024-06-01 or after 2024-06-30 do not count. That includes rows dated after the reference date.
  • A customer can have many transactions in the window. Sum all of them.
  • Customers with no transactions in the window do not appear in the output.
  • When customers tie on total_volume , the smaller customer_id ranks higher, so a tie at third place still yields exactly 3 rows.