Find customers in China whose order total exceeds 200 in the last 60 days

Quick Overview

Write a SQL query that finds customers from one country whose order totals over a fixed 60-day window exceed a threshold, sorted by spend with an id tie-break. It tests joins, the difference between WHERE and HAVING filters, inclusive timestamp boundaries and deterministic ordering.

Find customers in China whose order total exceeds 200 in the last 60 days

Company: Alibaba

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Find customers whose `country` is China and whose total order `amount` over the last 60 days exceeds 200. The current date is 2024-03-31, so the window is every order with `created_at >= '2024-01-31 00:00:00'`. Return each qualifying customer's `customer_id`, `name` and `total_spent`. ```hint Where each filter belongs Some conditions apply to a single order and some apply to a customer's total. Put each one in the clause that runs at that stage of the query. ``` ### Input Table(s) Table `customers` (primary key `customer_id`): | Column | Type | Meaning | |---|---|---| | customer_id | INT | Unique customer identifier | | name | VARCHAR(100), NOT NULL | Customer's name | | country | VARCHAR(50), NOT NULL | Customer's country | Table `orders` (primary key `order_id`): | Column | Type | Meaning | |---|---|---| | order_id | INT | Unique order identifier | | customer_id | INT, NOT NULL | Customer who placed the order (matches `customers.customer_id`) | | amount | DECIMAL(10,2), NOT NULL | Order amount | | created_at | DATETIME (TIMESTAMP in PostgreSQL), NOT NULL | When the order was placed | ### Output Contract - Columns, in this order: `customer_id`, `name`, `total_spent`. - `total_spent` is the sum of `amount` over the customer's orders with `created_at >= '2024-01-31 00:00:00'`. It is an exact two-decimal sum, so no rounding is applied. - Only customers with `country = 'China'` and `total_spent` strictly greater than 200 appear. - Sort by `total_spent` descending, then `customer_id` ascending. `customer_id` is unique, so the order is fully determined. ### Example `customers` | customer_id | name | country | |---|---|---| | 1 | Li Wei | China | | 2 | Zhang Min | China | | 3 | Wang Fang | China | | 4 | Chen Jie | China | | 5 | Maria Lopez | Spain | | 6 | Liu Yang | China | `orders` | order_id | customer_id | amount | created_at | |---|---|---|---| | 101 | 1 | 150.00 | 2024-02-10 09:15:00 | | 102 | 1 | 80.00 | 2024-03-20 18:40:00 | | 103 | 2 | 300.00 | 2024-01-30 23:59:59 | | 104 | 2 | 120.00 | 2024-02-05 12:00:00 | | 105 | 3 | 200.00 | 2024-01-31 00:00:00 | | 106 | 3 | 30.00 | 2024-03-01 10:00:00 | | 107 | 4 | 200.00 | 2024-03-15 08:00:00 | | 108 | 5 | 500.00 | 2024-03-10 14:30:00 | | 109 | 6 | 410.50 | 2024-03-30 20:00:00 | Expected output: | customer_id | name | total_spent | |---|---|---| | 6 | Liu Yang | 410.50 | | 1 | Li Wei | 230.00 | | 3 | Wang Fang | 230.00 | Customer 1 spent 150.00 + 80.00 = 230.00 inside the window. Customer 3's order 105 sits exactly on the cutoff and counts, giving 200.00 + 30.00 = 230.00, so customers 1 and 3 tie and are listed by `customer_id`. Customer 2's 300.00 order is one second before the cutoff, which leaves only 120.00. Customer 4 totals exactly 200.00, which does not exceed 200, and customer 5 is not in China. ### Constraints and Clarifications - The `customers` table has 1 to 10,000 rows and the `orders` table has 1 to 100,000 rows. - `amount` is non-negative and `country` is a non-empty string. Every column listed as NOT NULL is never NULL, so no NULL handling is needed. - The country match is the exact string `'China'`. - The window has only a lower bound, and it is inclusive: an order at exactly `2024-01-31 00:00:00` counts. Use this fixed timestamp, not the database clock. - Orders before the cutoff do not count toward the total, even when they would push a customer past 200. - A customer whose window total is exactly 200.00 does not qualify. - `orders.customer_id` is not declared as a foreign key. An order whose `customer_id` has no row in `customers` has no country and is ignored. - Two customers can share a name. They stay separate rows because the result is per `customer_id`.

Overview: Write a SQL query that finds customers from one country whose order totals over a fixed 60-day window exceed a threshold, sorted by spend with an id tie-break. It tests joins, the difference between WHERE and HAVING filters, inclusive timestamp boundaries and deterministic ordering.

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

Find customers whose country is China and whose total order amount over the last 60 days exceeds 200. The current date is 2024-03-31, so the window is every order with created_at >= '2024-01-31 00:00:00'. Return each qualifying customer's customer_id, name and total_spent.

Input Table(s)

Table customers (primary key customer_id):

ColumnTypeMeaning
customer_idINTUnique customer identifier
nameVARCHAR(100), NOT NULLCustomer's name
countryVARCHAR(50), NOT NULLCustomer's country

Table orders (primary key order_id):

ColumnTypeMeaning
order_idINTUnique order identifier
customer_idINT, NOT NULLCustomer who placed the order (matches customers.customer_id)
amountDECIMAL(10,2), NOT NULLOrder amount
created_atDATETIME (TIMESTAMP in PostgreSQL), NOT NULLWhen the order was placed

Output Contract

  • Columns, in this order: customer_id , name , total_spent .
  • total_spent is the sum of amount over the customer's orders with created_at >= '2024-01-31 00:00:00' . It is an exact two-decimal sum, so no rounding is applied.
  • Only customers with country = 'China' and total_spent strictly greater than 200 appear.
  • Sort by total_spent descending, then customer_id ascending. customer_id is unique, so the order is fully determined.

Example

customers

customer_idnamecountry
1Li WeiChina
2Zhang MinChina
3Wang FangChina
4Chen JieChina
5Maria LopezSpain
6Liu YangChina

orders

order_idcustomer_idamountcreated_at
1011150.002024-02-10 09:15:00
102180.002024-03-20 18:40:00
1032300.002024-01-30 23:59:59
1042120.002024-02-05 12:00:00
1053200.002024-01-31 00:00:00
106330.002024-03-01 10:00:00
1074200.002024-03-15 08:00:00
1085500.002024-03-10 14:30:00
1096410.502024-03-30 20:00:00

Expected output:

customer_idnametotal_spent
6Liu Yang410.50
1Li Wei230.00
3Wang Fang230.00

Customer 1 spent 150.00 + 80.00 = 230.00 inside the window. Customer 3's order 105 sits exactly on the cutoff and counts, giving 200.00 + 30.00 = 230.00, so customers 1 and 3 tie and are listed by customer_id. Customer 2's 300.00 order is one second before the cutoff, which leaves only 120.00. Customer 4 totals exactly 200.00, which does not exceed 200, and customer 5 is not in China.

Constraints and Clarifications

  • The customers table has 1 to 10,000 rows and the orders table has 1 to 100,000 rows.
  • amount is non-negative and country is a non-empty string. Every column listed as NOT NULL is never NULL, so no NULL handling is needed.
  • The country match is the exact string 'China' .
  • The window has only a lower bound, and it is inclusive: an order at exactly 2024-01-31 00:00:00 counts. Use this fixed timestamp, not the database clock.
  • Orders before the cutoff do not count toward the total, even when they would push a customer past 200.
  • A customer whose window total is exactly 200.00 does not qualify.
  • orders.customer_id is not declared as a foreign key. An order whose customer_id has no row in customers has no country and is ignored.
  • Two customers can share a name. They stay separate rows because the result is per customer_id .