Total Spend 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)`.
Report the total amount each diner spent, where a single order line contributes servings multiplied by unit_price. Order the result by total spend descending, and by `diner_id` ascending when two diners spent the same.
Return the columns `diner_id` and `total_spend`.
Tables
Example rows — the live problem includes the full dataset.
| order_id | dish_id | diner_id | servings |
|---|---|---|---|
| 1 | 1 | 101 | 10 |
| 2 | 2 | 101 | 1 |
| 3 | 3 | 102 | 3 |
| dish_id | unit_price |
|---|---|
| 1 | 10 |
| 2 | 25 |
| 3 | 15 |
Expected output
Your answer should return 3 rows with the columns diner_id, total_spend.
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