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.