SQL interview question: second-highest price in each group

EasyNth-highest value

Problem

Shared data rules

For each city and category, find the second-highest unit price among items sold in September 2026. Equal prices count as one value; skip a category that has only one price in the city.

Example

Input: orders
order_idstatuscitycreated_at
201deliveredBoston2026-09-03 12:00
202deliveredBoston2026-09-12 18:00
Input: order_items
order_idproduct_idcategoryquantityprice_usdRow note
20111Meat & seafood117.48
20211Meat & seafood217.48← same price — one value
20212Meat & seafood117.28
20131Beverages15.98
20232Beverages35.88
20121Dairy25.18
20221Dairy15.18← only $5.18 — no second price
Output
citycategorysecond_price_usd
BostonMeat & seafood17.28
BostonBeverages5.88

Meat: $17.48 (twice) and $17.28 — the second price is $17.28. Beverages: $5.98 and $5.88 — the second is $5.88.

Solve it free

Your query runs on PostgreSQL in your browser. No sign-up needed.

Skills it tests

More interview questions

All questions