AnalystPath

Top Earning Dish per Diner

SQLMediumMid level~15 min

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.

orders
order_iddish_iddiner_idservings
1110110
231017
311029
menu
dish_idunit_price
110
225
315

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

Related SQL questions