Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Start free
Rank Vehicles Within Regions
00:00
5 left

Rank Vehicles Within Regions

MediumSQL · PostgreSQL

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.

Output details

  • Use ascending order for region, regional_rank, and vehicle_id, placing NULL region values last.
  • Include sessions with session_date on or after DATE '2025-01-01' and before DATE '2025-04-01'.
  • Preserve the unrounded numeric sum as total_energy_kwh, using numeric zero for vehicles with no qualifying energy.

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
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the two tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.