Menu Item Totals Across Tickets
Problem
Table: `dish`
| Column Name | Type |
|---|---|
| dish_id | int |
| dish_name | varchar |
`dish_id` is the primary key.
Table: `ticket`
| Column Name | Type |
|---|---|
| ticket_id | int |
| dish_id | int |
| pending | int |
| served | int |
| comped | int |
| voided | int |
`ticket_id` is the primary key. Each ticket records, for one dish, the amount that is still pending, served, comped, and voided.
For every dish, report the total `pending`, `served`, `comped`, and `voided` amounts summed over all of its tickets. A dish with no tickets must still appear with totals of 0. Return the columns `dish_name`, `pending`, `served`, `comped`, `voided`, ordered by `dish_name`.
Tables
Example rows — the live problem includes the full dataset.
| dish_id | dish_name |
|---|---|
| 1 | Falafel |
| 2 | Hummus |
| 3 | Shakshuka |
| ticket_id | dish_id | pending | served | comped | voided |
|---|---|---|---|---|---|
| 88 | 1 | 10 | 5 | 0 | 1 |
| 89 | 1 | 2 | 3 | 1 | 0 |
| 90 | 2 | 4 | 4 | 2 | 2 |
Expected output
Your answer should return 3 rows with the columns dish_name, pending, served, comped, voided.
Starter code (SQL)
SELECT *
FROM dish;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