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.

Table `transactions` has one row per transaction. Its columns are `transaction_id` (INTEGER, the primary key, a unique transaction id), `customer_id` (INTEGER, the customer who made the transaction), `amount` (DECIMAL(10,2), the transaction amount, non-negative with up to 2 decimal places) and `transaction_date` (DATE, the date of the transaction). No column is NULL. 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`, both days included. Rows dated before `2024-06-01` or after `2024-06-30` do not count, and that includes rows dated after the reference date. A customer can have many transactions in the window: sum all of them. Return the columns `customer_id` and `total_volume`, in that order. `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, so do not round it further. Order the rows by `total_volume` descending, then `customer_id` ascending, and return at most 3 rows. When customers tie on `total_volume`, the smaller `customer_id` ranks higher, so a tie at third place still yields exactly 3 rows. Customers with no transactions in the window do not appear in the output. If fewer than 3 customers had transactions in the window, return only those.

Tables

transactions(transaction_id INTEGER, customer_id INTEGER, amount DECIMAL(10,2), transaction_date DATE)

Hints

  1. Count the days from your start date through `2024-06-30` with both ends included. You should get 30.
  2. The table also holds rows dated after the reference date. Make sure the window has an upper end as well as a lower one.
FiservData ScientisteasyData Manipulation (SQL/Python)

Top 3 Customers by Transaction Volume in the Last 30 Days

Table transactions has one row per transaction. Its columns are transaction_id (INTEGER, the primary key, a unique transaction id), customer_id (INTEGER, the customer who made the transaction), amount (DECIMAL(10,2), the transaction amount, non-negative with up to 2 decimal places) and transaction_date (DATE, the date of the transaction). No column is NULL.

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, both days included. Rows dated before 2024-06-01 or after 2024-06-30 do not count, and that includes rows dated after the reference date. A customer can have many transactions in the window: sum all of them.

Return the columns customer_id and total_volume, in that order. 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, so do not round it further. Order the rows by total_volume descending, then customer_id ascending, and return at most 3 rows. When customers tie on total_volume, the smaller customer_id ranks higher, so a tie at third place still yields exactly 3 rows. Customers with no transactions in the window do not appear in the output. If fewer than 3 customers had transactions in the window, return only those.

Expected Output

Tables

transactions
transactions
ColumnType
transaction_idINTEGER
customer_idINTEGER
amountDECIMAL(10,2)
transaction_dateDATE

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions