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.

ColumnTypeDescription
sku (key)TEXTItem code, such as 'HM-014'
outlet (key)TEXTOutlet name: 'Ridgeway', 'Bayside' or 'Midtown'
priceREALShelf price in dollars

Example

shelf_prices

skuoutletprice
HM-002Bayside14.5
HM-001Ridgeway9.99
HM-001Midtown10.49
HM-003Midtown42
HM-002Ridgeway13.75
HM-001Bayside9.49
HM-003Bayside39.9

Expected output

skuridgewaybaysidemidtown
HM-0019.999.4910.49
HM-00213.7514.5NULL
HM-003NULL39.942

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

One output row per sku means GROUP BY sku.
Inside each group, CASE WHEN outlet = 'Bayside' THEN price END keeps the Bayside price and turns every other row into NULL.
Wrap that CASE in an aggregate such as MAX to collapse the group to one value; aggregates skip NULLs.

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.

Practice next

Next Post Previous Post
No Comment
Add Comment
comment url