Summarize each customer's spending and assign a loyalty tier from orders, order items and products

Quick Overview

Write a SQL query that joins customers, orders, order items and products to report each customer's order count, total and average spend, last order date, category count and loyalty tier. It tests left join chains, distinct counts under join fan-out, NULL defaults for customers without orders, and conditional tiers.

Summarize each customer's spending and assign a loyalty tier from orders, order items and products

Company: IBM

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

An online store keeps its customers, products, orders and the line items of each order in four tables. For each customer, return `customer_id`, `customer_name` and a set of purchase metrics, including a loyalty tier based on total spending. Every customer must appear in the result, including customers who have never placed an order. ```hint Watch the row counts After joining an order to its line items, the order appears once per line item. Decide which of your metrics that repetition would distort and which it would not. ``` ### Input Table(s) `customers` | column | type | meaning | |---|---|---| | `customer_id` | INTEGER | Unique id of the customer. Primary key. | | `name` | VARCHAR(100) | Customer's name. Not null. | `products` | column | type | meaning | |---|---|---| | `product_id` | INTEGER | Unique id of the product. Primary key. | | `category` | VARCHAR(50) | Product category, such as `Electronics`. Not null. | | `price` | NUMERIC(10,2) | Current list price. Not null. | `orders` | column | type | meaning | |---|---|---| | `order_id` | INTEGER | Unique id of the order. Primary key. | | `customer_id` | INTEGER | Customer who placed the order. Foreign key to `customers`. Not null. | | `order_date` | DATE | Date the order was placed. Not null. | | `status` | VARCHAR(20) | One of `'pending'`, `'shipped'`, `'delivered'`, `'cancelled'`. Not null. | `order_items` | column | type | meaning | |---|---|---| | `order_item_id` | INTEGER | Unique id of the line item. Primary key. | | `order_id` | INTEGER | Order the line belongs to. Foreign key to `orders`. Not null. | | `product_id` | INTEGER | Product on the line. Foreign key to `products`. Not null. | | `quantity` | INTEGER | Units bought on this line. Not null. | | `unit_price` | NUMERIC(10,2) | Price charged per unit on this order. Not null. | ### Output Contract One row per customer, with these columns in this order: - `customer_id` - `customer_name`: the customer's `name`. - `total_orders`: number of distinct orders the customer placed. - `total_spent`: sum of `quantity * unit_price` over all of the customer's line items, to 2 decimal places. - `avg_order_value`: `total_spent` divided by `total_orders`, rounded to 2 decimal places. - `last_order_date`: the most recent `order_date`. - `unique_categories_purchased`: number of distinct product categories across the customer's line items. - `loyalty_tier`: `'Platinum'` when `total_spent` is at least 1000, `'Gold'` when it is at least 500, `'Silver'` when it is at least 100, and `'Bronze'` below 100. Sort by `total_spent` descending, then by `customer_id` ascending. ### Example `customers` | customer_id | name | |---|---| | 1 | Alice | | 2 | Bob | | 3 | Chen | | 4 | Dana | | 5 | Eve | `products` | product_id | category | price | |---|---|---| | 101 | Electronics | 300.00 | | 102 | Books | 20.00 | | 103 | Home | 45.50 | | 104 | Electronics | 80.00 | `orders` | order_id | customer_id | order_date | status | |---|---|---|---| | 1001 | 1 | 2024-01-05 | delivered | | 1002 | 1 | 2024-02-10 | shipped | | 1003 | 2 | 2024-01-20 | delivered | | 1004 | 2 | 2024-03-02 | cancelled | | 1005 | 3 | 2024-02-14 | pending | | 1006 | 5 | 2024-03-10 | pending | `order_items` | order_item_id | order_id | product_id | quantity | unit_price | |---|---|---|---|---| | 1 | 1001 | 101 | 3 | 300.00 | | 2 | 1001 | 102 | 3 | 20.00 | | 3 | 1002 | 104 | 1 | 75.00 | | 4 | 1003 | 103 | 2 | 45.50 | | 5 | 1004 | 102 | 1 | 20.00 | | 6 | 1005 | 101 | 2 | 280.00 | Expected result: | customer_id | customer_name | total_orders | total_spent | avg_order_value | last_order_date | unique_categories_purchased | loyalty_tier | |---|---|---|---|---|---|---|---| | 1 | Alice | 2 | 1035.00 | 517.50 | 2024-02-10 | 2 | Platinum | | 3 | Chen | 1 | 560.00 | 560.00 | 2024-02-14 | 1 | Gold | | 2 | Bob | 2 | 111.00 | 55.50 | 2024-03-02 | 2 | Silver | | 4 | Dana | 0 | 0.00 | 0.00 | NULL | 0 | Bronze | | 5 | Eve | 1 | 0.00 | 0.00 | 2024-03-10 | 0 | Bronze | Alice spent 900.00 + 60.00 + 75.00 = 1035.00 over 2 orders. Her line for product 104 is charged at its order price of 75.00, not the list price of 80.00. Her products fall into Electronics and Books, so she has 2 categories. Bob's cancelled order 1004 still counts, which gives him 2 orders, 111.00 spent and a last order date of 2024-03-02. Dana has no orders, so her numeric fields are 0 and her last order date is NULL. Eve has one order with no line items, so it counts as an order with zero spend. Dana and Eve tie at 0.00 and are ordered by `customer_id`. ### Constraints and Clarifications - `customers` and `products` each have 1 to 10,000 rows. `orders` and `order_items` each have 0 to 100,000 rows. - `unit_price` and `price` are positive with up to 2 decimal places, and `quantity` is at least 1. - Spending uses `order_items.unit_price`, the price charged on the order. `products.price` is not used in any metric. - Every order counts toward the metrics whatever its `status`, including `'cancelled'`. - A customer with no orders gets 0 for `total_orders`, `total_spent`, `avg_order_value` and `unique_categories_purchased`, NULL for `last_order_date`, and the tier `'Bronze'`. - An order with no line items counts as an order with zero spend. - Tier thresholds are inclusive: exactly 1000.00 is `'Platinum'`, exactly 500.00 is `'Gold'`, exactly 100.00 is `'Silver'`. - Customers with equal `total_spent` are ordered by `customer_id` ascending.

Overview: Write a SQL query that joins customers, orders, order items and products to report each customer's order count, total and average spend, last order date, category count and loyalty tier. It tests left join chains, distinct counts under join fan-out, NULL defaults for customers without orders, and conditional tiers.

IBM logo
IBM
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

An online store keeps its customers, products, orders and the line items of each order in four tables. For each customer, return customer_id, customer_name and a set of purchase metrics, including a loyalty tier based on total spending. Every customer must appear in the result, including customers who have never placed an order.

Input Table(s)

customers

columntypemeaning
customer_idINTEGERUnique id of the customer. Primary key.
nameVARCHAR(100)Customer's name. Not null.

products

columntypemeaning
product_idINTEGERUnique id of the product. Primary key.
categoryVARCHAR(50)Product category, such as Electronics. Not null.
priceNUMERIC(10,2)Current list price. Not null.

orders

columntypemeaning
order_idINTEGERUnique id of the order. Primary key.
customer_idINTEGERCustomer who placed the order. Foreign key to customers. Not null.
order_dateDATEDate the order was placed. Not null.
statusVARCHAR(20)One of 'pending', 'shipped', 'delivered', 'cancelled'. Not null.

order_items

columntypemeaning
order_item_idINTEGERUnique id of the line item. Primary key.
order_idINTEGEROrder the line belongs to. Foreign key to orders. Not null.
product_idINTEGERProduct on the line. Foreign key to products. Not null.
quantityINTEGERUnits bought on this line. Not null.
unit_priceNUMERIC(10,2)Price charged per unit on this order. Not null.

Output Contract

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

  • customer_id
  • customer_name : the customer's name .
  • total_orders : number of distinct orders the customer placed.
  • total_spent : sum of quantity * unit_price over all of the customer's line items, to 2 decimal places.
  • avg_order_value : total_spent divided by total_orders , rounded to 2 decimal places.
  • last_order_date : the most recent order_date .
  • unique_categories_purchased : number of distinct product categories across the customer's line items.
  • loyalty_tier : 'Platinum' when total_spent is at least 1000, 'Gold' when it is at least 500, 'Silver' when it is at least 100, and 'Bronze' below 100.

Sort by total_spent descending, then by customer_id ascending.

Example

customers

customer_idname
1Alice
2Bob
3Chen
4Dana
5Eve

products

product_idcategoryprice
101Electronics300.00
102Books20.00
103Home45.50
104Electronics80.00

orders

order_idcustomer_idorder_datestatus
100112024-01-05delivered
100212024-02-10shipped
100322024-01-20delivered
100422024-03-02cancelled
100532024-02-14pending
100652024-03-10pending

order_items

order_item_idorder_idproduct_idquantityunit_price
110011013300.00
21001102320.00
31002104175.00
41003103245.50
51004102120.00
610051012280.00

Expected result:

customer_idcustomer_nametotal_orderstotal_spentavg_order_valuelast_order_dateunique_categories_purchasedloyalty_tier
1Alice21035.00517.502024-02-102Platinum
3Chen1560.00560.002024-02-141Gold
2Bob2111.0055.502024-03-022Silver
4Dana00.000.00NULL0Bronze
5Eve10.000.002024-03-100Bronze

Alice spent 900.00 + 60.00 + 75.00 = 1035.00 over 2 orders. Her line for product 104 is charged at its order price of 75.00, not the list price of 80.00. Her products fall into Electronics and Books, so she has 2 categories. Bob's cancelled order 1004 still counts, which gives him 2 orders, 111.00 spent and a last order date of 2024-03-02. Dana has no orders, so her numeric fields are 0 and her last order date is NULL. Eve has one order with no line items, so it counts as an order with zero spend. Dana and Eve tie at 0.00 and are ordered by customer_id.

Constraints and Clarifications

  • customers and products each have 1 to 10,000 rows. orders and order_items each have 0 to 100,000 rows.
  • unit_price and price are positive with up to 2 decimal places, and quantity is at least 1.
  • Spending uses order_items.unit_price , the price charged on the order. products.price is not used in any metric.
  • Every order counts toward the metrics whatever its status , including 'cancelled' .
  • A customer with no orders gets 0 for total_orders , total_spent , avg_order_value and unique_categories_purchased , NULL for last_order_date , and the tier 'Bronze' .
  • An order with no line items counts as an order with zero spend.
  • Tier thresholds are inclusive: exactly 1000.00 is 'Platinum' , exactly 500.00 is 'Gold' , exactly 100.00 is 'Silver' .
  • Customers with equal total_spent are ordered by customer_id ascending.