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.
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: `customers`, `cars`, `mechanics`, `parts`, `appointments` and `service_parts`.
Write a PostgreSQL query that returns 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 tables:
- `customers(customer_id INT, name VARCHAR(100) NOT NULL)`. Primary key: `customer_id`. `name` is the customer's full name.
- `cars(car_id INT, customer_id INT NOT NULL, make VARCHAR(50) NOT NULL, model VARCHAR(50) NOT NULL, year INT NOT NULL)`. Primary key: `car_id`. `customer_id` is the customer who owns the car and references `customers.customer_id`. `make` is the manufacturer and `year` is the model year.
- `mechanics(mechanic_id INT, name VARCHAR(100) NOT NULL, specialization VARCHAR(50) NOT NULL)`. Primary key: `mechanic_id`. `name` is the mechanic's full name and `specialization` is the area of expertise.
- `parts(part_id INT, part_name VARCHAR(100) NOT NULL, unit_price DECIMAL(10,2) NOT NULL)`. Primary key: `part_id`. `unit_price` is the price of one unit.
- `appointments(appointment_id INT, customer_id INT NOT NULL, car_id INT NOT NULL, mechanic_id INT NOT NULL, service_date DATE NOT NULL, service_type VARCHAR(50) NOT NULL, status VARCHAR(20), labor_cost DECIMAL(10,2) NOT NULL)`. Primary key: `appointment_id`. `customer_id` is the customer who booked, `car_id` is the car being serviced and `mechanic_id` is the assigned mechanic; each references the table above. `status` is one of 'Scheduled', 'In Progress', 'Completed', 'Cancelled'. `labor_cost` is the labor charge for the appointment.
- `service_parts(appointment_id INT, part_id INT, quantity INT NOT NULL)`. Primary key: (`appointment_id`, `part_id`). `appointment_id` references `appointments` and `part_id` references `parts`. `quantity` is the units of that part used, 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.
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`.
Tables
customers(customer_id INTEGER, name VARCHAR(100))
cars(car_id INTEGER, customer_id INTEGER, make VARCHAR(50), model VARCHAR(50), year INTEGER)
mechanics(mechanic_id INTEGER, name VARCHAR(100), specialization VARCHAR(50))
parts(part_id INTEGER, part_name VARCHAR(100), unit_price DECIMAL(10,2))
appointments(appointment_id INTEGER, customer_id INTEGER, car_id INTEGER, mechanic_id INTEGER, service_date DATE, service_type VARCHAR(50), status VARCHAR(20), labor_cost DECIMAL(10,2))
service_parts(appointment_id INTEGER, part_id INTEGER, quantity INTEGER)
Hints
- 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.
- An appointment with no rows in `service_parts` still belongs in the result, with a parts cost of 0.00.