cswork / libraryOpen practice workspace

Coding questions · Waymo

SQL: Top 5 Vehicle Models by Miles per kWh Under a Maintenance Cost Cap

Write a PostgreSQL query returning the top 5 vehicle models by miles per kWh, limited to models with total maintenance cost under 5000 and safe when energy is 0 or NULL. Tests aggregating two child tables without join fan-out and NULL-safe division.

This SQL question was asked live in a first-round screen for a business intelligence role, with follow-ups on how the query handles NULLs and how it could be optimized.

Return the top 5 most efficient vehicle models, where a model's efficiency is its total miles driven divided by its total energy consumed in kWh (miles per kWh), computed across all vehicles of that model. Only include models whose total maintenance cost, across all vehicles of that model, is less than 5000. The query must be production-ready: it must not fail when energy is 0 or NULL. Vehicles that have never had a maintenance record are part of the answer too; they contribute a maintenance cost of 0.

The interview provided its own schema, which was not preserved. Use the following schema for this practice version.

Input Tables

vehicles: one row per vehicle.

ColumnTypeMeaning
vehicle_idINTEGERPrimary key
modelTEXTVehicle model name; never NULL

trips: one row per trip.

ColumnTypeMeaning
trip_idINTEGERPrimary key
vehicle_idINTEGERReferences vehicles.vehicle_id
miles_drivenNUMERICMiles driven on the trip; may be NULL
energy_kwhNUMERICEnergy consumed on the trip in kWh; may be NULL or 0

maintenance: one row per maintenance event.

ColumnTypeMeaning
maintenance_idINTEGERPrimary key
vehicle_idINTEGERReferences vehicles.vehicle_id
costNUMERICCost of the event; never NULL, never negative

Output Contract

  • Write one read-only PostgreSQL query (a single SELECT, optionally with CTEs).
  • For each model: total miles = sum of non-NULL miles_driven over all trips of its vehicles (0 if there are none); total energy = sum of non-NULL energy_kwh over the same trips; total maintenance = sum of cost over all maintenance rows of its vehicles (0 if there are none).
  • A model is eligible when its total maintenance is less than 5000 and its total energy is greater than 0. Models whose total energy is 0 or NULL (including models with no trips) have no defined efficiency and are excluded.
  • Return at most 5 rows with columns model (TEXT) and miles_per_kwh (NUMERIC, total miles divided by total energy, rounded to 2 decimal places).
  • Rank by the unrounded efficiency descending, breaking ties by model ascending, and return rows in that order. If fewer than 5 models are eligible, return all of them.

Example

vehicles

vehicle_idmodel
1Aurora
2Aurora
3Breeze
4Comet
5Dart

trips

trip_idvehicle_idmiles_drivenenergy_kwh
1110025
216015
324010
439020
53NULL5
641200
7430NULL
8530060

maintenance

maintenance_idvehicle_idcost
111500
211500
321200
455200
  • Aurora: 200 miles, 50 kWh, maintenance 4200: eligible, 4.00 miles per kWh.
  • Breeze: 90 miles, 25 kWh, no maintenance records (0): eligible, 3.60.
  • Comet: total energy 0: excluded.
  • Dart: 5.00 miles per kWh, but maintenance 5200: excluded.

Expected result:

modelmiles_per_kwh
Aurora4.00
Breeze3.60

Constraints and Clarifications

  • Totals are per model, not per vehicle: compute miles and energy across all trips of all the model's vehicles before dividing.
  • A trip with NULL miles_driven contributes nothing to miles but still contributes its energy_kwh, and the reverse.
  • The maintenance threshold is strict: a total of exactly 5000 is excluded.
  • model is unique per output row, and the tie rule makes the order total.

Hint: Count each fact once

Vehicle 1 has two trips and two maintenance events. Check how many times each of its miles and each of its costs would be added up by your joins.

Top 5 Vehicle Models by Miles per kWh Under a Maintenance Cost Cap

Table vehicles

columntypeconstraints
vehicle_idINTEGERPRIMARY KEY
modelTEXTNOT NULL

Sample data:

modelvehicle_id
Aurora1
Aurora2
Breeze3
Breeze4
Comet5
Drift6
Echo7
Ember8
Falcon9
Falcon10
Glide11
Lumen12
Nova13
Vector14
Zephyr15

Table trips

columntypeconstraints
trip_idINTEGERPRIMARY KEY
vehicle_idINTEGER
miles_drivenNUMERIC
energy_kwhNUMERIC

Sample data:

energy_kwhmiles_driventrip_idvehicle_id
3012011
208021
3010032
4018943
5NULL53
01263
012075
NULL3085
NULL5096
NULL40106
22100.04117
1880127
36160139
NULL20149
1445.21510
20NULL1611
12.5NULL1711
181001812
12601912
24902013
16602113
301742214
301862315

Table maintenance

columntypeconstraints
maintenance_idINTEGERPRIMARY KEY
vehicle_idINTEGER
costNUMERICNOT NULL

Sample data:

costmaintenance_idvehicle_id
150011
130021
120032
40045
25056
185067
80078
210089
950910
3001011
32001112
1799.991212
9001313
6501413
25001514
25001614
31001715
25001815

Expected output

modelmiles_per_kwh
Lumen5.33
Falcon4.5
Echo4.5
Breeze4.47
Aurora3.75

Hints

  1. Count each fact once: vehicle 1 has two trips and two maintenance events. Check how many times each of its miles and each of its costs would be added up by your joins.
  2. Totals are per model, not per vehicle: add up miles and energy across all trips of all the model's vehicles before dividing.
  3. Vehicles with no maintenance records still count, with a maintenance cost of 0, and a model whose total energy is 0 or NULL has no defined efficiency.