cswork / libraryOpen practice workspace

Coding questions · Capital One

Find top category per region in Aug 2025

This question evaluates proficiency in SQL data manipulation skills such as joining related tables, aggregating revenue, filtering by date and status, and implementing deterministic tie-breaking for categories.

You are given the following schema and sample data.

Schema:

  • customers(customer_id INT, name TEXT, region TEXT)
  • orders(order_id INT, customer_id INT, order_date DATE, status TEXT)
  • order_items(order_id INT, product_id INT, qty INT, unit_price DECIMAL(10,2))
  • products(product_id INT, category TEXT)

Sample tables:

customers
+-------------+-------+--------+
| customer_id | name | region |
+-------------+-------+--------+
| 1 | Alice | East |
| 2 | Bob | East |
| 3 | Cara | West |
| 4 | Dan | West |
+-------------+-------+--------+
products
+------------+----------+
| product_id | category |
+------------+----------+
| 10 | Gadgets |
| 11 | Gadgets |
| 12 | Widgets |
+------------+----------+
orders
+----------+-------------+------------+----------+
| order_id | customer_id | order_date | status |
+----------+-------------+------------+----------+
| 101 | 1 | 2025-08-05 | Delivered|
| 102 | 1 | 2025-08-20 | Cancelled|
| 103 | 2 | 2025-08-21 | Delivered|
| 104 | 3 | 2025-08-25 | Delivered|
| 105 | 4 | 2025-08-30 | Delivered|
| 106 | 3 | 2025-09-02 | Delivered|
+----------+-------------+------------+----------+
order_items
+----------+------------+-----+------------+
| order_id | product_id | qty | unit_price |
+----------+------------+-----+------------+
| 101 | 10 | 2 | 50.00 |
| 101 | 12 | 1 | 80.00 |
| 103 | 10 | 1 | 60.00 |
| 103 | 12 | 3 | 30.00 |
| 104 | 11 | 2 | 40.00 |
| 104 | 12 | 1 | 100.00 |
| 105 | 12 | 4 | 25.00 |
| 106 | 10 | 5 | 10.00 |
+----------+------------+-----+------------+

Task: Write a single SQL statement that returns, for each region, the product category with the highest total delivered revenue in August 2025 (2025-08-01 to 2025-08-31). Revenue is SUM(qty*unit_price) from order_items, but only include orders with status='Delivered' and order_date in August 2025. Output columns: region, category, total_revenue. Break ties by choosing the lexicographically smallest category. You must use exactly one subquery or one CTE (no more), and you may not use window functions.

Top product category by delivered revenue per region (Aug 2025)

Given the tables customers, orders, order_items, and products, write a single SQL statement that returns, for each customer region, the product category with the highest total delivered revenue in August 2025 (from 2025-08-01 to 2025-08-31 inclusive).

Revenue is computed as SUM(qty * unit_price) from order_items, but only for orders where status = 'Delivered' and order_date is within August 2025.

Output columns:

  • region
  • category
  • total_revenue

Tie-break rule: if multiple categories have the same highest total_revenue within a region, return the lexicographically smallest category.

Constraint: You must use exactly one CTE or exactly one subquery (no more than one), and you may not use window functions.

Table customers

columntypeconstraints
customer_idINTPRIMARY KEY, NOT NULL
nameVARCHAR(100)NOT NULL
regionVARCHAR(20)NOT NULL

Sample data:

customer_idnameregion
1AliceEast
2BobEast
3CaraWest
4DanWest

Table products

columntypeconstraints
product_idINTPRIMARY KEY, NOT NULL
categoryVARCHAR(50)NOT NULL

Sample data:

categoryproduct_id
Gadgets10
Gadgets11
Widgets12

Table orders

columntypeconstraints
order_idINTPRIMARY KEY, NOT NULL
customer_idINTNOT NULL
order_dateDATENOT NULL
statusVARCHAR(20)NOT NULL

Sample data:

customer_idorder_dateorder_idstatus
12025-08-05101Delivered
12025-08-20102Cancelled
22025-08-21103Delivered
32025-08-25104Delivered
42025-08-30105Delivered
32025-09-02106Delivered

Table order_items

columntypeconstraints
order_idINTNOT NULL
product_idINTNOT NULL
qtyINTNOT NULL
unit_priceDECIMAL(10,2)NOT NULL

Sample data:

order_idproduct_idqtyunit_price
10110250
10112180
10310160
10312330
10411240
104121100
10512425
10610510

Expected output

One row per region showing the category with the highest total delivered revenue in August 2025, with ties broken by lexicographically smallest category.

categoryregiontotal_revenue
WidgetsEast170
WidgetsWest200

Hints

  1. First aggregate revenue by (region, category) for Delivered orders in 2025-08-01 to 2025-08-31.
  2. To pick the top category per region without window functions, use a self-join anti-join pattern that eliminates rows beaten by a higher revenue (or same revenue but lexicographically smaller category).