Tool Prices Across Outlets
A hardware chain stores shelf prices as one row per item and outlet. The buying team wants a side-by-side view instead: one row per sku that appears in the table, with one price column per outlet.
Return the columns sku, ridgeway, bayside, midtown, holding the item's price at the outlets 'Ridgeway', 'Bayside' and 'Midtown'. If an outlet does not stock the item, that column is NULL (not 0). Sort by sku ascending. An empty table gives no rows.
Tables
shelf_prices — One row per item stocked at an outlet; an item missing for an outlet is not stocked there.
| Column | Type | Description |
|---|---|---|
sku (key) | TEXT | Item code, such as 'HM-014' |
outlet (key) | TEXT | Outlet name: 'Ridgeway', 'Bayside' or 'Midtown' |
price | REAL | Shelf price in dollars |
Example
shelf_prices
| sku | outlet | price |
|---|---|---|
| HM-002 | Bayside | 14.5 |
| HM-001 | Ridgeway | 9.99 |
| HM-001 | Midtown | 10.49 |
| HM-003 | Midtown | 42 |
| HM-002 | Ridgeway | 13.75 |
| HM-001 | Bayside | 9.49 |
| HM-003 | Bayside | 39.9 |
Expected output
| sku | ridgeway | bayside | midtown |
|---|---|---|---|
| HM-001 | 9.99 | 9.49 | 10.49 |
| HM-002 | 13.75 | 14.5 | NULL |
| HM-003 | NULL | 39.9 | 42 |
Return the rows in exactly the order described. Your query also runs against hidden datasets with tricky edge cases.
SQL Path · Strings & CASE · problem 58 of 70 · SQLite
The idea
Pivoting turns values of one column (outlet) into separate columns. Group by sku so each item becomes one row, then build each outlet column with conditional aggregation: MAX(CASE WHEN outlet = 'Bayside' THEN price END).
Why it works: the CASE has no ELSE, so for rows of other outlets it yields NULL. Because (sku, outlet) is the primary key, each group has at most one non-NULL value per outlet, and MAX simply picks it. If the outlet has no row, every input is NULL and MAX returns NULL, which is exactly "not stocked". Adding ELSE 0 would destroy that distinction and confuse a missing item with a genuinely free one.
The alternative first lists every distinct sku and then LEFT JOINs the price table once per outlet. Starting from the full item list, rather than from one outlet, keeps items that some outlets do not stock.
Solution
SELECT sku,
MAX(CASE WHEN outlet = 'Ridgeway' THEN price END) AS ridgeway,
MAX(CASE WHEN outlet = 'Bayside' THEN price END) AS bayside,
MAX(CASE WHEN outlet = 'Midtown' THEN price END) AS midtown
FROM shelf_prices
GROUP BY sku
ORDER BY sku;
Another way
SELECT s.sku,
r.price AS ridgeway,
b.price AS bayside,
m.price AS midtown
FROM (SELECT DISTINCT sku FROM shelf_prices) AS s
LEFT JOIN shelf_prices AS r ON r.sku = s.sku AND r.outlet = 'Ridgeway'
LEFT JOIN shelf_prices AS b ON b.sku = s.sku AND b.outlet = 'Bayside'
LEFT JOIN shelf_prices AS m ON m.sku = s.sku AND m.outlet = 'Midtown'
ORDER BY s.sku;
Why common answers fail
ELSE 0 turns "not stocked" into a price of 0 instead of NULL.
SELECT sku, SUM(CASE WHEN outlet = 'Ridgeway' THEN price ELSE 0 END) AS ridgeway, SUM(CASE WHEN outlet = 'Bayside' THEN price ELSE 0 END) AS bayside, SUM(CASE WHEN outlet = 'Midtown' THEN price ELSE 0 END) AS midtown FROM shelf_prices GROUP BY sku ORDER BY sku;
Starts from the Ridgeway rows, so items that Ridgeway does not stock vanish from the report.
SELECT r.sku, r.price AS ridgeway, b.price AS bayside, m.price AS midtown FROM shelf_prices r LEFT JOIN shelf_prices b ON b.sku = r.sku AND b.outlet = 'Bayside' LEFT JOIN shelf_prices m ON m.sku = r.sku AND m.outlet = 'Midtown' WHERE r.outlet = 'Ridgeway' ORDER BY r.sku;
Uses CASE without an aggregate, so each column is computed from a single arbitrary row of the group and most prices come out NULL.
SELECT sku, CASE WHEN outlet = 'Ridgeway' THEN price END AS ridgeway, CASE WHEN outlet = 'Bayside' THEN price END AS bayside, CASE WHEN outlet = 'Midtown' THEN price END AS midtown FROM shelf_prices GROUP BY sku ORDER BY sku;
What the interviewer is checking
Conditional aggregation is the portable way to pivot; the interviewer also watches whether you keep missing data as NULL instead of 0.
Mistakes to avoid
- ELSE 0 inside the CASE, which reports unstocked items as free.
- Driving the query from one outlet's rows and losing items it does not stock.
- Writing the CASE without an aggregate: SQLite accepts it but reads only one row per group.
- Forgetting ORDER BY when the order is part of the requirement.
Follow-ups to expect
- Add a column with the cheapest outlet for each item (compare the three columns, or use a window function before pivoting).
- What if new outlets open every month? (Pivot columns must be known in advance; generate the SQL dynamically or keep the long format.)
- Count how many outlets stock each item (COUNT(*) per sku).
In real work
Price comparison sheets, A/B metric tables and month-by-column finance reports are built from long tables with exactly this conditional aggregation.