Interview questions · Meta
Write SQL for car rental utilization by city
This question evaluates proficiency in SQL querying and relational data modeling, focusing on time-interval overlap logic, distinct aggregation across joined tables, null handling, and safe computation of utilization metrics.
SQL / Data Query Prompt (Car Rental)
You are given four tables:
user
user_id
location
location_idcity
car
car_idcar_size(e.g., compact, midsize, suv)location_id(the car’s current/home location)is_active(1 if available in fleet)
fct_rental
rental_idcar_idpickup_location_idpickup_tsdropoff_ts(nullable if not yet returned)
Task
For a given date D (e.g., '2025-01-15'), compute for each (city, car_size):
rented_cars: how many distinct cars were rented at any time during date D (i.e., the rental interval overlaps that date).inventory_cars: how many cars of thatcar_sizeexist in that city’s inventory (based oncar.location_id, onlyis_active = 1).utilization_rate = rented_cars / inventory_cars(as a decimal).
Return rows grouped by city and car_size.
Notes
- A rental overlaps date D if it started before the end of D and ended after the start of D. Treat
dropoff_tsNULL as "still ongoing". - If
inventory_cars = 0, returnutilization_rateas NULL (or avoid division-by-zero).
Community answers
Nothing here yet.
Join in on CSWork →