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

orders: 10 of 200 rows
order_idstatusregionprofit
10097PendingEU69.37
10194CancelledAPAC88.74
10291ShippedLATAM108.11
10388ProcessingMEA127.48
10485ReturnedUS146.85
10582On HoldEU166.22
10679CompletedAPAC185.59
10776PendingLATAM204.96
10873CancelledMEA224.33
10970ShippedUS243.7

Expected output

First 5 of 33 rows
regionstatusorder_count
APACCancelled6
EUCancelled6
LATAMCancelled6
MEACompleted6
USPending6

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

Filter rows, count pairs, filter pairs, sort
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 DESC

The WHERE acts on orders; the HAVING acts on pairs.

Common follow-up questions