Build a per-appointment service report with customer, car, mechanic, parts cost and total cost

Quick Overview

A SQL reporting question that asks for one row per service appointment with customer, car and mechanic details plus parts, labor and total cost. It tests joining six tables, adding up parts line items without duplicating appointment rows, keeping appointments that used no parts, and NULL handling.

Build a per-appointment service report with customer, car, mechanic, parts cost and total cost

Company: Tesla

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

A car service center stores its customers, their cars, its mechanics, a parts catalog, service appointments and the parts used on each appointment in six tables. Return one row per appointment with the customer's name, the car's `make`, `model` and `year`, the mechanic's name and `specialization`, the `service_date`, `service_type` and `status`, and the appointment's parts cost, labor cost and total cost. The parts cost is the sum of `unit_price * quantity` over the parts used on the appointment, or 0 when no parts were used, and the total cost is the labor cost plus the parts cost. ```hint Watch the row count Count how many rows an appointment that used two different parts becomes once `service_parts` is joined in, and check what that does to any per-appointment value you add up. ``` ### Input Table(s) `customers`. Primary key: `customer_id`. | Column | Type | Meaning | |---|---|---| | customer_id | INT | Customer identifier (primary key). | | name | VARCHAR(100) | Customer's full name, NOT NULL. | `cars`. Primary key: `car_id`. Foreign key: `customer_id` references `customers.customer_id`. | Column | Type | Meaning | |---|---|---| | car_id | INT | Car identifier (primary key). | | customer_id | INT | The customer who owns the car, NOT NULL. | | make | VARCHAR(50) | Manufacturer, NOT NULL. | | model | VARCHAR(50) | Model name, NOT NULL. | | year | INT | Model year, NOT NULL. | `mechanics`. Primary key: `mechanic_id`. | Column | Type | Meaning | |---|---|---| | mechanic_id | INT | Mechanic identifier (primary key). | | name | VARCHAR(100) | Mechanic's full name, NOT NULL. | | specialization | VARCHAR(50) | Area of expertise, NOT NULL. | `parts`. Primary key: `part_id`. | Column | Type | Meaning | |---|---|---| | part_id | INT | Part identifier (primary key). | | part_name | VARCHAR(100) | Part description, NOT NULL. | | unit_price | DECIMAL(10,2) | Price of one unit, NOT NULL. | `appointments`. Primary key: `appointment_id`. Foreign keys: `customer_id`, `car_id` and `mechanic_id` reference the tables above. | Column | Type | Meaning | |---|---|---| | appointment_id | INT | Appointment identifier (primary key). | | customer_id | INT | The customer who booked, NOT NULL. | | car_id | INT | The car being serviced, NOT NULL. | | mechanic_id | INT | The assigned mechanic, NOT NULL. | | service_date | DATE | Date of the service, NOT NULL. | | service_type | VARCHAR(50) | Kind of service, NOT NULL. | | status | VARCHAR(20) | One of 'Scheduled', 'In Progress', 'Completed', 'Cancelled'. | | labor_cost | DECIMAL(10,2) | Labor charge for the appointment, NOT NULL. | `service_parts`. Primary key: (`appointment_id`, `part_id`). Foreign keys: `appointment_id` references `appointments`, `part_id` references `parts`. | Column | Type | Meaning | |---|---|---| | appointment_id | INT | The appointment that used the part. | | part_id | INT | The part used. | | quantity | INT | Units of that part used, NOT NULL and at least 1. | ### Output Contract - Columns, in this order: `appointment_id`, `customer_name`, `make`, `model`, `year`, `mechanic_name`, `specialization`, `service_date`, `service_type`, `status`, `parts_cost`, `labor_cost`, `total_cost`. - `parts_cost` is the sum of `unit_price * quantity` over the appointment's rows in `service_parts`, or 0.00 when the appointment has none. - `total_cost` is `labor_cost + parts_cost`. - All three cost columns are exact decimals with 2 places. No further rounding is needed. - Exactly one row per appointment in `appointments`, ordered by `service_date` descending, then `appointment_id` ascending. ### Example `customers` | customer_id | name | |---|---| | 1 | Ana Ruiz | | 2 | Ben Okafor | `cars` | car_id | customer_id | make | model | year | |---|---|---|---|---| | 10 | 1 | Toyota | Camry | 2019 | | 11 | 2 | Honda | Civic | 2021 | `mechanics` | mechanic_id | name | specialization | |---|---|---| | 100 | Dana Lee | Brakes | | 101 | Sam Patel | Electrical | `parts` | part_id | part_name | unit_price | |---|---|---| | 500 | Brake pad set | 45.00 | | 501 | Brake rotor | 80.00 | | 502 | 12V battery | 150.00 | `appointments` | appointment_id | customer_id | car_id | mechanic_id | service_date | service_type | status | labor_cost | |---|---|---|---|---|---|---|---| | 1 | 1 | 10 | 100 | 2024-03-01 | Brake service | Completed | 120.00 | | 2 | 2 | 11 | 101 | 2024-03-05 | Battery replacement | Completed | 60.00 | | 3 | 1 | 10 | 101 | 2024-03-05 | Diagnostics | Scheduled | 90.00 | | 4 | 2 | 11 | 100 | 2024-02-20 | Brake inspection | Cancelled | 0.00 | `service_parts` | appointment_id | part_id | quantity | |---|---|---| | 1 | 500 | 2 | | 1 | 501 | 2 | | 2 | 502 | 1 | Expected result: | appointment_id | customer_name | make | model | year | mechanic_name | specialization | service_date | service_type | status | parts_cost | labor_cost | total_cost | |---|---|---|---|---|---|---|---|---|---|---|---|---| | 2 | Ben Okafor | Honda | Civic | 2021 | Sam Patel | Electrical | 2024-03-05 | Battery replacement | Completed | 150.00 | 60.00 | 210.00 | | 3 | Ana Ruiz | Toyota | Camry | 2019 | Sam Patel | Electrical | 2024-03-05 | Diagnostics | Scheduled | 0.00 | 90.00 | 90.00 | | 1 | Ana Ruiz | Toyota | Camry | 2019 | Dana Lee | Brakes | 2024-03-01 | Brake service | Completed | 250.00 | 120.00 | 370.00 | | 4 | Ben Okafor | Honda | Civic | 2021 | Dana Lee | Brakes | 2024-02-20 | Brake inspection | Cancelled | 0.00 | 0.00 | 0.00 | Appointment 1 used two brake pad sets and two rotors, so its parts cost is 2 × 45.00 + 2 × 80.00 = 250.00 and its total is 120.00 + 250.00 = 370.00. Appointments 3 and 4 used no parts and still appear, with a parts cost of 0.00. Appointments 2 and 3 share the latest date, so the lower `appointment_id` comes first. ### Constraints and Clarifications - Each table has at most 10,000 rows. - Each car belongs to exactly one customer. Take the customer's name from the appointment's `customer_id`. - Each appointment references one customer, one car and one mechanic, all present in their tables, and uses zero or more parts through `service_parts`. - A part appears at most once per appointment in `service_parts` (the primary key), with its total `quantity` on that row. - Every appointment is returned whatever its `status`, including 'Cancelled' ones, with the costs as recorded. - No cost, price or quantity column is NULL. The only missing value to handle is an appointment with no rows in `service_parts`.

Overview: A SQL reporting question that asks for one row per service appointment with customer, car and mechanic details plus parts, labor and total cost. It tests joining six tables, adding up parts line items without duplicating appointment rows, keeping appointments that used no parts, and NULL handling.

Tesla logo
Tesla
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

A car service center stores its customers, their cars, its mechanics, a parts catalog, service appointments and the parts used on each appointment in six tables.

Return one row per appointment with the customer's name, the car's make, model and year, the mechanic's name and specialization, the service_date, service_type and status, and the appointment's parts cost, labor cost and total cost. The parts cost is the sum of unit_price * quantity over the parts used on the appointment, or 0 when no parts were used, and the total cost is the labor cost plus the parts cost.

Input Table(s)

customers. Primary key: customer_id.

ColumnTypeMeaning
customer_idINTCustomer identifier (primary key).
nameVARCHAR(100)Customer's full name, NOT NULL.

cars. Primary key: car_id. Foreign key: customer_id references customers.customer_id.

ColumnTypeMeaning
car_idINTCar identifier (primary key).
customer_idINTThe customer who owns the car, NOT NULL.
makeVARCHAR(50)Manufacturer, NOT NULL.
modelVARCHAR(50)Model name, NOT NULL.
yearINTModel year, NOT NULL.

mechanics. Primary key: mechanic_id.

ColumnTypeMeaning
mechanic_idINTMechanic identifier (primary key).
nameVARCHAR(100)Mechanic's full name, NOT NULL.
specializationVARCHAR(50)Area of expertise, NOT NULL.

parts. Primary key: part_id.

ColumnTypeMeaning
part_idINTPart identifier (primary key).
part_nameVARCHAR(100)Part description, NOT NULL.
unit_priceDECIMAL(10,2)Price of one unit, NOT NULL.

appointments. Primary key: appointment_id. Foreign keys: customer_id, car_id and mechanic_id reference the tables above.

ColumnTypeMeaning
appointment_idINTAppointment identifier (primary key).
customer_idINTThe customer who booked, NOT NULL.
car_idINTThe car being serviced, NOT NULL.
mechanic_idINTThe assigned mechanic, NOT NULL.
service_dateDATEDate of the service, NOT NULL.
service_typeVARCHAR(50)Kind of service, NOT NULL.
statusVARCHAR(20)One of 'Scheduled', 'In Progress', 'Completed', 'Cancelled'.
labor_costDECIMAL(10,2)Labor charge for the appointment, NOT NULL.

service_parts. Primary key: (appointment_id, part_id). Foreign keys: appointment_id references appointments, part_id references parts.

ColumnTypeMeaning
appointment_idINTThe appointment that used the part.
part_idINTThe part used.
quantityINTUnits of that part used, NOT NULL and at least 1.

Output Contract

  • Columns, in this order: appointment_id , customer_name , make , model , year , mechanic_name , specialization , service_date , service_type , status , parts_cost , labor_cost , total_cost .
  • parts_cost is the sum of unit_price * quantity over the appointment's rows in service_parts , or 0.00 when the appointment has none.
  • total_cost is labor_cost + parts_cost .
  • All three cost columns are exact decimals with 2 places. No further rounding is needed.
  • Exactly one row per appointment in appointments , ordered by service_date descending, then appointment_id ascending.

Example

customers

customer_idname
1Ana Ruiz
2Ben Okafor

cars

car_idcustomer_idmakemodelyear
101ToyotaCamry2019
112HondaCivic2021

mechanics

mechanic_idnamespecialization
100Dana LeeBrakes
101Sam PatelElectrical

parts

part_idpart_nameunit_price
500Brake pad set45.00
501Brake rotor80.00
50212V battery150.00

appointments

appointment_idcustomer_idcar_idmechanic_idservice_dateservice_typestatuslabor_cost
11101002024-03-01Brake serviceCompleted120.00
22111012024-03-05Battery replacementCompleted60.00
31101012024-03-05DiagnosticsScheduled90.00
42111002024-02-20Brake inspectionCancelled0.00

service_parts

appointment_idpart_idquantity
15002
15012
25021

Expected result:

appointment_idcustomer_namemakemodelyearmechanic_namespecializationservice_dateservice_typestatusparts_costlabor_costtotal_cost
2Ben OkaforHondaCivic2021Sam PatelElectrical2024-03-05Battery replacementCompleted150.0060.00210.00
3Ana RuizToyotaCamry2019Sam PatelElectrical2024-03-05DiagnosticsScheduled0.0090.0090.00
1Ana RuizToyotaCamry2019Dana LeeBrakes2024-03-01Brake serviceCompleted250.00120.00370.00
4Ben OkaforHondaCivic2021Dana LeeBrakes2024-02-20Brake inspectionCancelled0.000.000.00

Appointment 1 used two brake pad sets and two rotors, so its parts cost is 2 × 45.00 + 2 × 80.00 = 250.00 and its total is 120.00 + 250.00 = 370.00. Appointments 3 and 4 used no parts and still appear, with a parts cost of 0.00. Appointments 2 and 3 share the latest date, so the lower appointment_id comes first.

Constraints and Clarifications

  • Each table has at most 10,000 rows.
  • Each car belongs to exactly one customer. Take the customer's name from the appointment's customer_id .
  • Each appointment references one customer, one car and one mechanic, all present in their tables, and uses zero or more parts through service_parts .
  • A part appears at most once per appointment in service_parts (the primary key), with its total quantity on that row.
  • Every appointment is returned whatever its status , including 'Cancelled' ones, with the costs as recorded.
  • No cost, price or quantity column is NULL. The only missing value to handle is an appointment with no rows in service_parts .