Selling Where Nobody Lives
A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.
- Domain
- SQL
- Difficulty
- medium
- Seniority
- L4
Problem
An order with no destination on file belongs to no market, and only a shipped or completed order counts as a landed sale. From orders, list each region (region) that has landed at least one sale clearing more than $1,200 in profit.
Shipments land in regions our customer list has never heard of.
Tables
customers
Columns: customer_id INTEGER, first_name TEXT, last_name TEXT, country TEXT
| customer_id | first_name | last_name | country |
|---|---|---|---|
| 40089 | Priya | Gupta | India |
| 40178 | Luca | Rossi | Italy |
| 40267 | Ava | Nguyen | Vietnam |
| 40356 | Ethan | Johnson | Spain |
| 40445 | Maria | Garcia | China |
| 40534 | Chen | Wei | Russia |
| 40623 | Sofia | Petrova | Ireland |
| 40712 | Liam | O'Connor | Mexico |
| 40801 | Isabella | Martinez | Canada |
| 40890 | Noah | Brown | France |
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 |
|---|
| LATAM |
| US |
| APAC |
| MEA |
Worked solution and explanation
What the question is really asking
Strip the marquee-sale story and this is a set-membership question: which destination regions contain at least one order that clears two bars at once, a fulfilled status ('Shipped' or 'Completed') and more than $1,200 of profit? The skill is applying both conditions to the SAME order row, then collapsing the survivors to one row per region. Miss that the two conditions must co-occur and 'EU' walks in, either on a $1,425 order that was still 'Pending' or on fulfilled orders worth a few hundred dollars each.
Why both conditions must land on the same row
A region earns its place only if a single order was both fulfilled and high-value. Filter profit on its own and you credit 'EU' for a $1,409 order that was 'Returned'. Filter status on its own and you credit regions whose fulfilled orders were small. Summing profit per region makes the same mistake from another direction, because it scores the region's total instead of any one order.
- Profit filter alone (wrong)
SELECT DISTINCT region FROM orders WHERE profit > 1200returns 'EU' off a 'Pending' order and a NULLregionfrom a high-value order with no destination.- Both conditions on the same order (correct)
o.status IN ('Shipped', 'Completed'),o.region IS NOT NULLando.order_id IN (SELECT order_id ...)all sit in oneWHERE, so a region stays only when one order clears every bar.
Step 1: Keep only fulfilled orders
status IN ('Shipped', 'Completed') restricts the rows to orders that actually reached the customer. It is a two-value set, so a single equality loses half the fulfilled orders, and dropping it lets a 'Cancelled' or 'Returned' order qualify a region.
Step 2: Require the high-value test on the same order
o.order_id IN (SELECT order_id FROM orders WHERE profit > 1200) is a semi-join to the set of marquee orders. Because it matches on order_id, the profit bar and the fulfilled status must hold for the same order, not two different ones.
Step 3: Guard the missing region, then collapse
o.region IS NOT NULL drops orders with no destination on file, and DISTINCT reduces each surviving region to one row. The answer is a set of markets, so row order is not part of it.
SELECT DISTINCT o.region
FROM orders o
WHERE o.status IN ('Shipped', 'Completed')
AND o.region IS NOT NULL
AND o.order_id IN (
SELECT order_id
FROM orders
WHERE profit > 1200
)
;The IN subquery is a semi-join to the high-value orders; matching on order_id forces the profit and status conditions onto the same row.
Common follow-up questions
- How would the query change if 'high-value' were defined in a separate `flagged_orders` table instead of a profit threshold? (Tests whether they see the `IN` subquery as a reusable semi-join rather than a dressed-up profit predicate.)
- Rewrite the high-value condition as an `EXISTS` and as a `JOIN`, and explain which you would ship. (Checks fluency with the equivalent semi-join forms and their NULL and duplicate behavior.)
- How would you also report how many marquee orders each of these regions landed? (Extends the membership filter into a per-region count over the qualifying set.)