Average power consumed per vehicle model and service year

Quick Overview

Write a SQL query that joins a vehicles table to its service records and reports the average power consumed for each vehicle model in each service year, rounded to two decimals. It tests joins, grouping on an extracted year, aggregate rounding and deterministic ordering.

Average power consumed per vehicle model and service year

Company: Tesla

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Join the `vehicles` and `service_data` tables on `vehicle_id` and compute the average `power_consumed` for each vehicle `model` in each service year, where the service year is the calendar year of `service_date`. Round the average to 2 decimal places. Return `model`, `service_year` and `avg_power_consumed`, ordered by `model` and then `service_year`, both ascending. ```hint Pick the grouping key Several vehicles can share one model. Decide what a single output row stands for before you write the GROUP BY. ``` ### Input Table(s) `vehicles` | Column | Type | Meaning | |---|---|---| | vehicle_id | INT | Primary key, one row per vehicle | | model | VARCHAR(100) | Model name, NOT NULL. Many vehicles can share a model. | | battery_capacity | DECIMAL(10,2) | Battery capacity. Nullable and not needed for this query. | `service_data` | Column | Type | Meaning | |---|---|---| | service_id | INT | Primary key, one row per service record | | vehicle_id | INT | NOT NULL, foreign key to `vehicles.vehicle_id` | | service_date | DATE | Date of the service, NOT NULL | | power_consumed | DECIMAL(10,2) | Power consumed recorded for this service, NOT NULL | Keys: `vehicles.vehicle_id` and `service_data.service_id` are primary keys. `service_data.vehicle_id` references `vehicles.vehicle_id`, and a vehicle can have many service records. ### Output Contract | Column | Type | Meaning | |---|---|---| | model | VARCHAR | The vehicle model | | service_year | INTEGER | Calendar year of `service_date` | | avg_power_consumed | NUMERIC | Mean of `power_consumed` over all service records of that model in that year, rounded to 2 decimals | - One row per (`model`, `service_year`) pair that has at least one service record. - Order by `model` ascending, then `service_year` ascending. The pair is unique in the output, so the order is fully determined. - Round the final average once, half away from zero, as PostgreSQL `ROUND` does on numeric values. ### Example `vehicles` | vehicle_id | model | battery_capacity | |---|---|---| | 1 | Aster | 75.00 | | 2 | Aster | 82.00 | | 3 | Birch | NULL | | 4 | Cedar | 100.00 | `service_data` | service_id | vehicle_id | service_date | power_consumed | |---|---|---|---| | 101 | 1 | 2022-03-14 | 310.50 | | 102 | 1 | 2022-07-01 | 300.00 | | 103 | 2 | 2022-11-02 | 295.20 | | 104 | 1 | 2023-01-20 | 320.00 | | 105 | 3 | 2022-06-30 | 410.00 | | 106 | 3 | 2023-02-10 | 402.40 | | 107 | 3 | 2023-08-15 | 398.10 | | 108 | 3 | 2023-12-31 | 400.00 | Expected result: | model | service_year | avg_power_consumed | |---|---|---| | Aster | 2022 | 301.90 | | Aster | 2023 | 320.00 | | Birch | 2022 | 410.00 | | Birch | 2023 | 400.17 | Aster in 2022 combines three records from vehicles 1 and 2: (310.50 + 300.00 + 295.20) / 3 = 301.90. Birch in 2023 averages 1200.50 / 3 = 400.1666..., which rounds to 400.17. Cedar has no service records, so it does not appear. ### Constraints and Clarifications - `vehicles` has at most 100 rows and `service_data` at most 10,000 rows. - `power_consumed` is always positive and never NULL, so every service record counts toward its group's average. - Every `vehicle_id` in `service_data` exists in `vehicles`. - A model with no service records does not appear in the output. - `battery_capacity` may be NULL. It has no effect on the result. - Every service record carries equal weight in the average, whichever vehicle it belongs to. Do not average per-vehicle averages. - Two service records on the same date are two separate records, and both count.

Overview: Write a SQL query that joins a vehicles table to its service records and reports the average power consumed for each vehicle model in each service year, rounded to two decimals. It tests joins, grouping on an extracted year, aggregate rounding and deterministic ordering.

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

Join the vehicles and service_data tables on vehicle_id and compute the average power_consumed for each vehicle model in each service year, where the service year is the calendar year of service_date. Round the average to 2 decimal places. Return model, service_year and avg_power_consumed, ordered by model and then service_year, both ascending.

Input Table(s)

vehicles

ColumnTypeMeaning
vehicle_idINTPrimary key, one row per vehicle
modelVARCHAR(100)Model name, NOT NULL. Many vehicles can share a model.
battery_capacityDECIMAL(10,2)Battery capacity. Nullable and not needed for this query.

service_data

ColumnTypeMeaning
service_idINTPrimary key, one row per service record
vehicle_idINTNOT NULL, foreign key to vehicles.vehicle_id
service_dateDATEDate of the service, NOT NULL
power_consumedDECIMAL(10,2)Power consumed recorded for this service, NOT NULL

Keys: vehicles.vehicle_id and service_data.service_id are primary keys. service_data.vehicle_id references vehicles.vehicle_id, and a vehicle can have many service records.

Output Contract

ColumnTypeMeaning
modelVARCHARThe vehicle model
service_yearINTEGERCalendar year of service_date
avg_power_consumedNUMERICMean of power_consumed over all service records of that model in that year, rounded to 2 decimals
  • One row per ( model , service_year ) pair that has at least one service record.
  • Order by model ascending, then service_year ascending. The pair is unique in the output, so the order is fully determined.
  • Round the final average once, half away from zero, as PostgreSQL ROUND does on numeric values.

Example

vehicles

vehicle_idmodelbattery_capacity
1Aster75.00
2Aster82.00
3BirchNULL
4Cedar100.00

service_data

service_idvehicle_idservice_datepower_consumed
10112022-03-14310.50
10212022-07-01300.00
10322022-11-02295.20
10412023-01-20320.00
10532022-06-30410.00
10632023-02-10402.40
10732023-08-15398.10
10832023-12-31400.00

Expected result:

modelservice_yearavg_power_consumed
Aster2022301.90
Aster2023320.00
Birch2022410.00
Birch2023400.17

Aster in 2022 combines three records from vehicles 1 and 2: (310.50 + 300.00 + 295.20) / 3 = 301.90. Birch in 2023 averages 1200.50 / 3 = 400.1666..., which rounds to 400.17. Cedar has no service records, so it does not appear.

Constraints and Clarifications

  • vehicles has at most 100 rows and service_data at most 10,000 rows.
  • power_consumed is always positive and never NULL, so every service record counts toward its group's average.
  • Every vehicle_id in service_data exists in vehicles .
  • A model with no service records does not appear in the output.
  • battery_capacity may be NULL. It has no effect on the result.
  • Every service record carries equal weight in the average, whichever vehicle it belongs to. Do not average per-vehicle averages.
  • Two service records on the same date are two separate records, and both count.