Itemize the Largest Repair Ticket
Problem
An auto repair shop builds tickets out of parts. Table `parts` has `(part_id, rate)` giving each part's price, and table `ticket_lines` has `(ticket_id, part_id, qty)` listing the parts used on each ticket.
Find the ticket whose total cost is the highest, where a ticket's total is the sum over its lines of qty multiplied by rate. If two tickets tie on total cost, choose the one with the smaller `ticket_id`. Then itemize that ticket: for each of its lines, show the part, the quantity, and the line cost (qty times rate).
Return the columns `part_id`, `qty`, and `line_cost`.
Tables
Example rows — the live problem includes the full dataset.
| part_id | rate |
|---|---|
| 1 | 100 |
| 2 | 200 |
| ticket_id | part_id | qty |
|---|---|---|
| 1 | 1 | 2 |
| 3 | 2 | 1 |
| 2 | 2 | 3 |
Expected output
Your answer should return 2 rows with the columns part_id, qty, line_cost.
Starter code (SQL)
SELECT *
FROM parts;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