Top Earning Dish per Diner
Problem
A restaurant tracks every order line and the menu price of each dish. Table `orders` has `(order_id, dish_id, diner_id, servings)`, and table `menu` has `(dish_id, unit_price)`.
For each diner, report the dish on which they spent the most money in total (sum over all their order lines of servings multiplied by unit_price). If a diner's maximum spend is tied across several dishes, report every one of those dishes.
Return the columns `diner_id` and `dish_id`.
Tables
Example rows — the live problem includes the full dataset.
| order_id | dish_id | diner_id | servings |
|---|---|---|---|
| 1 | 1 | 101 | 10 |
| 2 | 3 | 101 | 7 |
| 3 | 1 | 102 | 9 |
| dish_id | unit_price |
|---|---|
| 1 | 10 |
| 2 | 25 |
| 3 | 15 |
Expected output
Your answer should return 4 rows with the columns diner_id, dish_id.
Starter code (SQL)
SELECT *
FROM orders;Solve this SQL question free
Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock