AnalystPath

Itemize the Largest Repair Ticket

SQLHardSenior level~15 min

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.

parts
part_idrate
1100
2200
ticket_lines
ticket_idpart_idqty
112
321
223

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

Related SQL questions