Find high spenders by 30-day total spend and distinct transaction days

Quick Overview

Write a SQL query that finds customers whose spending in a fixed 30-day window is above 500 and spread over at least three distinct calendar days. It tests half-open timestamp ranges, counting distinct dates, HAVING filters on aggregates, and deterministic ordering with a tie-breaker.

Find high spenders by 30-day total spend and distinct transaction days

Company: Amazon

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Find high spenders in `transactions` within the last 30 days before the fixed reference date 2024-03-16, meaning `transaction_date >= '2024-02-15 00:00:00'` and `transaction_date < '2024-03-16 00:00:00'`. A customer qualifies only if both of these hold for their transactions in that window: - The total `amount` is strictly greater than 500. - They transacted on at least 3 distinct calendar days. ```hint Two edge cases to test Check what your day count does when a customer buys twice on the same day, and what your window does with a purchase at exactly midnight on 2024-03-16. ``` ### Input Table(s) `transactions` (primary key `transaction_id`) | Column | Type | Meaning | |---|---|---| | transaction_id | INT | Transaction identifier | | customer_id | INT | Customer who made the transaction, not null | | amount | DECIMAL(10,2) | Transaction amount, not null | | transaction_date | DATETIME (TIMESTAMP in PostgreSQL) | Date and time of the transaction, not null | ### Output Contract - Columns, in this order: `customer_id`, `total_spent`, `unique_days`. - `total_spent` is the sum of `amount` over the customer's transactions in the window, rounded to 2 decimals. - `unique_days` is the number of distinct calendar days in the window on which the customer transacted. - One row per qualifying customer. - Sort by `total_spent` descending, then `customer_id` ascending. `customer_id` is unique in the output, so the order is fully determined. ### Example `transactions` | transaction_id | customer_id | amount | transaction_date | |---|---|---|---| | 1 | 1 | 200.00 | 2024-02-15 00:00:00 | | 2 | 1 | 150.00 | 2024-02-20 14:30:00 | | 3 | 1 | 200.00 | 2024-03-01 09:15:00 | | 4 | 2 | 300.00 | 2024-03-02 10:00:00 | | 5 | 2 | 250.00 | 2024-03-02 18:00:00 | | 6 | 2 | 100.00 | 2024-03-05 12:00:00 | | 7 | 3 | 100.00 | 2024-02-16 08:00:00 | | 8 | 3 | 150.00 | 2024-02-25 13:00:00 | | 9 | 3 | 250.00 | 2024-03-10 19:45:00 | | 10 | 4 | 400.00 | 2024-02-14 23:59:59 | | 11 | 4 | 120.00 | 2024-02-18 11:00:00 | | 12 | 4 | 180.00 | 2024-02-28 16:00:00 | | 13 | 4 | 250.00 | 2024-03-15 22:10:00 | | 14 | 4 | 300.00 | 2024-03-16 00:00:00 | | 15 | 5 | 400.00 | 2024-02-20 10:00:00 | | 16 | 5 | 300.25 | 2024-02-29 15:00:00 | | 17 | 5 | 99.50 | 2024-03-12 09:00:00 | Expected result: | customer_id | total_spent | unique_days | |---|---|---| | 5 | 799.75 | 3 | | 1 | 550.00 | 3 | | 4 | 550.00 | 3 | Customer 2 spent 650.00, but two of the purchases fall on 2024-03-02, so only 2 distinct days. Customer 3 spent exactly 500.00, which is not more than 500. For customer 4, transaction 10 falls before the window and transaction 14 falls on the excluded end boundary, leaving 550.00 over 3 days. Customers 1 and 4 tie at 550.00, so the lower `customer_id` comes first. ### Constraints and Clarifications - The window is half-open: it includes 2024-02-15 00:00:00 and excludes 2024-03-16 00:00:00. The reference date is fixed; do not compute it from the current date. - A calendar day is the date part of `transaction_date` as stored, with no time zone conversion. Several transactions on the same day count as one day. - Only transactions inside the window count toward the total and the day count. - No column is NULL. - `transaction_id` is unique. Two rows with the same customer, amount and time are separate transactions and both count. - The table has 1 to 100,000 rows. - `amount` is positive with up to 2 decimal places.

Overview: Write a SQL query that finds customers whose spending in a fixed 30-day window is above 500 and spread over at least three distinct calendar days. It tests half-open timestamp ranges, counting distinct dates, HAVING filters on aggregates, and deterministic ordering with a tie-breaker.

Amazon logo
Amazon
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Find high spenders in transactions within the last 30 days before the fixed reference date 2024-03-16, meaning transaction_date >= '2024-02-15 00:00:00' and transaction_date < '2024-03-16 00:00:00'.

A customer qualifies only if both of these hold for their transactions in that window:

  • The total amount is strictly greater than 500.
  • They transacted on at least 3 distinct calendar days.

Input Table(s)

transactions (primary key transaction_id)

ColumnTypeMeaning
transaction_idINTTransaction identifier
customer_idINTCustomer who made the transaction, not null
amountDECIMAL(10,2)Transaction amount, not null
transaction_dateDATETIME (TIMESTAMP in PostgreSQL)Date and time of the transaction, not null

Output Contract

  • Columns, in this order: customer_id , total_spent , unique_days .
  • total_spent is the sum of amount over the customer's transactions in the window, rounded to 2 decimals.
  • unique_days is the number of distinct calendar days in the window on which the customer transacted.
  • One row per qualifying customer.
  • Sort by total_spent descending, then customer_id ascending. customer_id is unique in the output, so the order is fully determined.

Example

transactions

transaction_idcustomer_idamounttransaction_date
11200.002024-02-15 00:00:00
21150.002024-02-20 14:30:00
31200.002024-03-01 09:15:00
42300.002024-03-02 10:00:00
52250.002024-03-02 18:00:00
62100.002024-03-05 12:00:00
73100.002024-02-16 08:00:00
83150.002024-02-25 13:00:00
93250.002024-03-10 19:45:00
104400.002024-02-14 23:59:59
114120.002024-02-18 11:00:00
124180.002024-02-28 16:00:00
134250.002024-03-15 22:10:00
144300.002024-03-16 00:00:00
155400.002024-02-20 10:00:00
165300.252024-02-29 15:00:00
17599.502024-03-12 09:00:00

Expected result:

customer_idtotal_spentunique_days
5799.753
1550.003
4550.003

Customer 2 spent 650.00, but two of the purchases fall on 2024-03-02, so only 2 distinct days. Customer 3 spent exactly 500.00, which is not more than 500. For customer 4, transaction 10 falls before the window and transaction 14 falls on the excluded end boundary, leaving 550.00 over 3 days. Customers 1 and 4 tie at 550.00, so the lower customer_id comes first.

Constraints and Clarifications

  • The window is half-open: it includes 2024-02-15 00:00:00 and excludes 2024-03-16 00:00:00. The reference date is fixed; do not compute it from the current date.
  • A calendar day is the date part of transaction_date as stored, with no time zone conversion. Several transactions on the same day count as one day.
  • Only transactions inside the window count toward the total and the day count.
  • No column is NULL.
  • transaction_id is unique. Two rows with the same customer, amount and time are separate transactions and both count.
  • The table has 1 to 100,000 rows.
  • amount is positive with up to 2 decimal places.