Top 3 Customers by Total Spend in a Date Range

Quick Overview

Write a SQL query that returns the three customers with the highest total order amount inside a half-open January 2024 date window, breaking ties by customer id. It tests joining two tables, filtering dates before aggregating, SUM with GROUP BY, rounding, and deterministic ordering with LIMIT.

Top 3 Customers by Total Spend in a Date Range

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

You have two tables, `customers` and `orders`. Return the top 3 customers by total spend, counting only orders with `order_date` on or after `'2024-01-01'` and strictly before `'2024-01-31'`. Output `customer_id`, `name` and `total_spend` (rounded to 2 decimal places). Ties on total spend are broken by smaller `customer_id` first. ```hint Order of operations Decide which rows to drop before you add anything up, and make sure the sort fully decides the order before you cut the list to three rows. ``` ### Input Table(s) Table `customers`, primary key `customer_id`: | Column | Type | Meaning | |---|---|---| | customer_id | INT | Customer identifier (primary key) | | name | VARCHAR(100) | Customer name, NOT NULL | Table `orders`, primary key `order_id`. `customer_id` is a foreign key to `customers.customer_id`: | Column | Type | Meaning | |---|---|---| | order_id | INT | Order identifier (primary key) | | customer_id | INT | Customer who placed the order | | amount | DECIMAL(10,2) | Order amount, positive, NOT NULL | | order_date | DATE | Date of the order, NOT NULL | ### Output Contract - Columns, in this order: `customer_id`, `name`, `total_spend`. - `total_spend` is the sum of `amount` over the customer's orders inside the window, rounded to 2 decimal places. - Sort by `total_spend` descending, then by `customer_id` ascending. - Return at most 3 rows. When fewer than 3 customers have an order in the window, return only those customers. ### Example `customers` | customer_id | name | |---|---| | 1 | Alice | | 2 | Bob | | 3 | Chen | | 4 | Dana | | 5 | Eli | `orders` | order_id | customer_id | amount | order_date | |---|---|---|---| | 101 | 1 | 120.00 | 2024-01-05 | | 102 | 2 | 200.00 | 2024-01-10 | | 103 | 1 | 80.00 | 2024-01-20 | | 104 | 3 | 150.00 | 2024-01-31 | | 105 | 3 | 90.50 | 2024-01-15 | | 106 | 4 | 500.00 | 2023-12-31 | | 107 | 4 | 60.25 | 2024-01-02 | | 108 | 5 | 90.50 | 2024-01-30 | Expected result: | customer_id | name | total_spend | |---|---|---| | 1 | Alice | 200.00 | | 2 | Bob | 200.00 | | 3 | Chen | 90.50 | Order 104 is dated `2024-01-31` and order 106 is dated `2023-12-31`, so both fall outside the window. The remaining totals are Alice 200.00, Bob 200.00, Chen 90.50, Eli 90.50 and Dana 60.25. Alice and Bob tie, and so do Chen and Eli. The smaller `customer_id` wins each tie, so Eli (5) misses the cut. ### Constraints and Clarifications - `customers` has at most 10,000 rows and `orders` at most 100,000 rows. - `amount` is a positive decimal and `order_date` is a valid date. Neither column contains NULLs. - Every `customer_id` in `orders` references a row in `customers`. - The window is half-open. Orders dated `2024-01-31` are excluded, along with anything before `2024-01-01`. - A customer with several orders in the window gets one row holding the sum. Customers with no order in the window are left out. - Ties on `total_spend` are broken by smaller `customer_id`. Because `customer_id` is the primary key, the order of the result is unique.

Overview: Write a SQL query that returns the three customers with the highest total order amount inside a half-open January 2024 date window, breaking ties by customer id. It tests joining two tables, filtering dates before aggregating, SUM with GROUP BY, rounding, and deterministic ordering with LIMIT.

Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

You have two tables, customers and orders. Return the top 3 customers by total spend, counting only orders with order_date on or after '2024-01-01' and strictly before '2024-01-31'. Output customer_id, name and total_spend (rounded to 2 decimal places). Ties on total spend are broken by smaller customer_id first.

Input Table(s)

Table customers, primary key customer_id:

ColumnTypeMeaning
customer_idINTCustomer identifier (primary key)
nameVARCHAR(100)Customer name, NOT NULL

Table orders, primary key order_id. customer_id is a foreign key to customers.customer_id:

ColumnTypeMeaning
order_idINTOrder identifier (primary key)
customer_idINTCustomer who placed the order
amountDECIMAL(10,2)Order amount, positive, NOT NULL
order_dateDATEDate of the order, NOT NULL

Output Contract

  • Columns, in this order: customer_id , name , total_spend .
  • total_spend is the sum of amount over the customer's orders inside the window, rounded to 2 decimal places.
  • Sort by total_spend descending, then by customer_id ascending.
  • Return at most 3 rows. When fewer than 3 customers have an order in the window, return only those customers.

Example

customers

customer_idname
1Alice
2Bob
3Chen
4Dana
5Eli

orders

order_idcustomer_idamountorder_date
1011120.002024-01-05
1022200.002024-01-10
103180.002024-01-20
1043150.002024-01-31
105390.502024-01-15
1064500.002023-12-31
107460.252024-01-02
108590.502024-01-30

Expected result:

customer_idnametotal_spend
1Alice200.00
2Bob200.00
3Chen90.50

Order 104 is dated 2024-01-31 and order 106 is dated 2023-12-31, so both fall outside the window. The remaining totals are Alice 200.00, Bob 200.00, Chen 90.50, Eli 90.50 and Dana 60.25. Alice and Bob tie, and so do Chen and Eli. The smaller customer_id wins each tie, so Eli (5) misses the cut.

Constraints and Clarifications

  • customers has at most 10,000 rows and orders at most 100,000 rows.
  • amount is a positive decimal and order_date is a valid date. Neither column contains NULLs.
  • Every customer_id in orders references a row in customers .
  • The window is half-open. Orders dated 2024-01-31 are excluded, along with anything before 2024-01-01 .
  • A customer with several orders in the window gets one row holding the sum. Customers with no order in the window are left out.
  • Ties on total_spend are broken by smaller customer_id . Because customer_id is the primary key, the order of the result is unique.