Find the bugs: the highest-priced item in each order
Find the item with the highest unit price in each order. If several items share the highest price, return all of them. Mark the lines that contain errors.
Find the errorsSubqueries, CTEs, EXISTS / IN, UNIONMiddleAnalystData EngineerBackend
Input data · iv_order_items
| id | order_id | product | quantity | price |
|---|---|---|---|---|
| 1 | 1 | pen | 10 | 2.00 |
| 2 | 1 | notebook | 1 | 10.00 |
| 3 | 2 | pen | 1 | 4.00 |
| 4 | 3 | notebook | 5 | 10.00 |
| 5 | 3 | bag | 1 | 50.00 |
| 6 | 4 | pen | 2 | 3.00 |
| 7 | 5 | pen | 3 | 10.00 |
| 8 | 7 | bag | 2 | 60.00 |
| 9 | 8 | notebook | 2 | 40.00 |
SELECT order_id, product, price
FROM iv_order_items
WHERE price IN (
SELECT MAX(price)
FROM iv_order_items
GROUP BY order_id
)
ORDER BY order_id, product