Segment Customers into Quartiles by Total Spend

Quick Overview

Write a SQL query that totals each customer's order spend, including customers with no orders, and assigns spend quartiles with NTILE(4). It tests LEFT JOIN aggregation, COALESCE for NULL totals, and a deterministic window ordering that breaks ties.

Segment Customers into Quartiles by Total Spend

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

For every customer, compute `total_spend` (the sum of their order amounts) and assign `spend_quartile` with `NTILE(4)`. Return `customer_id`, `customer_name`, `total_spend` and `spend_quartile`. Quartile 1 holds the highest spenders and quartile 4 the lowest. Break spend ties by the smaller `customer_id` first. Customers with no orders, and orders whose `amount` is NULL, contribute 0. Round `total_spend` to 2 decimals. ```hint Missing customers Check which join keeps a customer who never ordered, and what `SUM` returns for that customer. ``` ### Input Table(s) `customers` | Column | Type | Meaning | |---|---|---| | customer_id | INT | Customer id | | customer_name | VARCHAR(100) | Customer name, never NULL | Primary key: `customer_id`. `orders` | Column | Type | Meaning | |---|---|---| | order_id | INT | Order id | | customer_id | INT | Customer who placed the order | | amount | DECIMAL(10,2) | Order amount, can be NULL | Primary key: `order_id`. Foreign key: `orders.customer_id` references `customers.customer_id`. ### Output Contract One row per customer, with these columns in this order: | Column | Meaning | |---|---| | customer_id | Customer id | | customer_name | Customer name | | total_spend | Sum of the customer's order amounts, 0 when there is nothing to sum, rounded to 2 decimals | | spend_quartile | `NTILE(4)` bucket over all customers ordered by `total_spend` descending, then `customer_id` ascending | Order the rows by `total_spend` descending, then `customer_id` ascending. ### Example `customers` | customer_id | customer_name | |---|---| | 1 | Alice | | 2 | Bob | | 3 | Carol | | 4 | Dave | `orders` | order_id | customer_id | amount | |---|---|---| | 1 | 1 | 150.50 | | 2 | 1 | 49.50 | | 3 | 2 | 100.00 | | 4 | 4 | NULL | Expected result: | customer_id | customer_name | total_spend | spend_quartile | |---|---|---|---| | 1 | Alice | 200.00 | 1 | | 2 | Bob | 100.00 | 2 | | 3 | Carol | 0.00 | 3 | | 4 | Dave | 0.00 | 4 | Alice totals 150.50 + 49.50 = 200.00 and Bob totals 100.00. Carol has no orders and Dave's only order has a NULL amount, so both total 0.00, and Carol comes first because her id is smaller. With 4 customers, `NTILE(4)` puts one customer in each quartile. ### Constraints and Clarifications - There are 1 to 10,000 customers. `customer_id` and `order_id` are unique. - A customer can have any number of orders, including none. All of a customer's orders are summed into one row. - A NULL `amount` adds nothing to the sum. A customer with no orders, or with only NULL amounts, has `total_spend` 0.00. - When the number of customers is not divisible by 4, the lower-numbered quartiles take one extra customer each, as `NTILE` does: 5 customers split 2, 1, 1, 1 and 6 customers split 2, 2, 1, 1. With fewer than 4 customers, only quartiles 1 up to the customer count appear. - Customers with equal spend can land in different quartiles. The one with the smaller `customer_id` takes the earlier position.

Overview: Write a SQL query that totals each customer's order spend, including customers with no orders, and assigns spend quartiles with NTILE(4). It tests LEFT JOIN aggregation, COALESCE for NULL totals, and a deterministic window ordering that breaks ties.

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

For every customer, compute total_spend (the sum of their order amounts) and assign spend_quartile with NTILE(4). Return customer_id, customer_name, total_spend and spend_quartile.

Quartile 1 holds the highest spenders and quartile 4 the lowest. Break spend ties by the smaller customer_id first. Customers with no orders, and orders whose amount is NULL, contribute 0. Round total_spend to 2 decimals.

Input Table(s)

customers

ColumnTypeMeaning
customer_idINTCustomer id
customer_nameVARCHAR(100)Customer name, never NULL

Primary key: customer_id.

orders

ColumnTypeMeaning
order_idINTOrder id
customer_idINTCustomer who placed the order
amountDECIMAL(10,2)Order amount, can be NULL

Primary key: order_id. Foreign key: orders.customer_id references customers.customer_id.

Output Contract

One row per customer, with these columns in this order:

ColumnMeaning
customer_idCustomer id
customer_nameCustomer name
total_spendSum of the customer's order amounts, 0 when there is nothing to sum, rounded to 2 decimals
spend_quartileNTILE(4) bucket over all customers ordered by total_spend descending, then customer_id ascending

Order the rows by total_spend descending, then customer_id ascending.

Example

customers

customer_idcustomer_name
1Alice
2Bob
3Carol
4Dave

orders

order_idcustomer_idamount
11150.50
2149.50
32100.00
44NULL

Expected result:

customer_idcustomer_nametotal_spendspend_quartile
1Alice200.001
2Bob100.002
3Carol0.003
4Dave0.004

Alice totals 150.50 + 49.50 = 200.00 and Bob totals 100.00. Carol has no orders and Dave's only order has a NULL amount, so both total 0.00, and Carol comes first because her id is smaller. With 4 customers, NTILE(4) puts one customer in each quartile.

Constraints and Clarifications

  • There are 1 to 10,000 customers. customer_id and order_id are unique.
  • A customer can have any number of orders, including none. All of a customer's orders are summed into one row.
  • A NULL amount adds nothing to the sum. A customer with no orders, or with only NULL amounts, has total_spend 0.00.
  • When the number of customers is not divisible by 4, the lower-numbered quartiles take one extra customer each, as NTILE does: 5 customers split 2, 1, 1, 1 and 6 customers split 2, 2, 1, 1. With fewer than 4 customers, only quartiles 1 up to the customer count appear.
  • Customers with equal spend can land in different quartiles. The one with the smaller customer_id takes the earlier position.