Regional Status
An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.
- Domain
- SQL
- Difficulty
- easy
- Seniority
- L3
Problem
The operations team is diagnosing fulfillment bottlenecks across regions. For each region and order status combination that has valid data on both fields, show the number of orders. Only surface combinations that account for at least 3 orders, and list the results from the most common combination to the least.
The full regional breakdown.
Tables
orders
Columns: order_id INTEGER, status TEXT, region TEXT, profit REAL
| order_id | status | region | profit |
|---|---|---|---|
| 10097 | Pending | EU | 69.37 |
| 10194 | Cancelled | APAC | 88.74 |
| 10291 | Shipped | LATAM | 108.11 |
| 10388 | Processing | MEA | 127.48 |
| 10485 | Returned | US | 146.85 |
| 10582 | On Hold | EU | 166.22 |
| 10679 | Completed | APAC | 185.59 |
| 10776 | Pending | LATAM | 204.96 |
| 10873 | Cancelled | MEA | 224.33 |
| 10970 | Shipped | US | 243.7 |
Expected output
| region | status | order_count |
|---|---|---|
| APAC | Cancelled | 6 |
| EU | Cancelled | 6 |
| LATAM | Cancelled | 6 |
| MEA | Completed | 6 |
| US | Pending | 6 |
Worked solution and explanation
What this really is
Beneath the operations framing this is a two-key count with a size cutoff on the groups, and it has 2 places to go wrong. Anyone writes GROUP BY region, status. What separates candidates is knowing that grouping keeps a missing key as a key of its own: the 18 orders with no region and the 14 with no status form their own buckets, and some of them clear the 3-order bar. The second trap is where that bar lives. The count does not exist until the rows are grouped, so a row filter cannot test it.
Build it in 4 moves
Step 1: Drop orders missing either key
Put region IS NOT NULL AND status IS NOT NULL in WHERE, so incomplete orders never reach a bucket. On this seed the NULL-region orders alone form a (NULL, 'Processing') bucket of 4 orders, which would pass the cutoff and show up as a row nobody can act on.
Step 2: Count each region and status pair
GROUP BY region, status gives one bucket per pair that actually occurs, and COUNT(*) tallies its orders. With the NULLs gone, COUNT(*) and COUNT(region) agree, but COUNT(*) says what the question means: orders.
Step 3: Apply the cutoff to the buckets
HAVING COUNT(*) >= 3 runs after grouping. Here it removes 2 pairs, 'EU' with 'Shipped' and 'LATAM' with 'Returned', each holding 2 orders, which is why the answer has 33 rows.
Step 4: Sort on the count alone
ORDER BY order_count DESC delivers most common first. Counts on this seed are only 6 or 4, so many pairs tie, and the question says nothing about how tied pairs line up: any order among them is correct. Tiebreak columns would answer a question nobody asked.
The solution
SELECT
region,
status,
COUNT(*) AS order_count
FROM orders
WHERE region IS NOT NULL
AND status IS NOT NULL
GROUP BY region, status
HAVING COUNT(*) >= 3
ORDER BY order_count DESCThe WHERE acts on orders; the HAVING acts on pairs.
Common follow-up questions
- What changes if the team wanted only the single most common combination? (A `LIMIT 1` cuts between 16 pairs tied at 6, so the statement would owe a tie rule; `DENSE_RANK()` keeps every tied pair instead.)
- How would you also return total profit per combination? (Add `SUM(profit) AS total_profit`; `SUM` skips NULLs and still adds the zero and negative profits this table holds.)
- Where would a cutoff on total profit go? (In the same `HAVING`, joined with `AND`, since it also depends on the group.)