Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rank Vehicles Within Regions

EasySQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Rank Vehicles Within Regions. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Lucid Motors wants to compare vehicle charging activity across geographic regions. Write a PostgreSQL query that ranks registered Lucid vehicles by total charging energy during Q1 2025.

Requirements

  1. Include every vehicle registered in the vehicles table, including vehicles with no qualifying charging sessions.
  2. Calculate each vehicle's total energy_kwh from charging sessions between January 1 and March 31, 2025. Treat missing or entirely NULL energy values as zero.
  3. Rank vehicles within their region using RANK(), with the highest total energy receiving rank 1. Preserve ties with the same rank.
  4. Return region, vehicle_id, model, total_energy_kwh, and regional_rank, ordered by region, rank, and vehicle ID.

Schema

vehicles
ColumnTypeDescription
vehicle_idPKINTUnique vehicle identifier
modelVARCHAR(50)Lucid vehicle model and trim
regionVARCHAR(50)Sales or service region
charging_sessions
ColumnTypeDescription
session_idPKINTUnique charging session identifier
vehicle_idINTVehicle associated with the session
session_dateDATEDate of the charging session
energy_kwhNUMERIC(8,2)Energy delivered during the session
Tablesvehiclescharging_sessions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results