AnalystPath

EV Charging Spend per Vehicle

SQLMediumMid level~15 min

Problem

A network of charging stations records each visit in charging_sessions(station_id, vehicle_id, plug_in, plug_out, amount_paid). For every vehicle, return total_paid (the sum of all amounts it paid), avg_hourly_cost (total amount paid divided by total hours plugged in, rounded to 2 decimals), and top_station (the station_id where the vehicle spent the most total time; if two stations are tied, choose the smaller station_id). Order the result by vehicle_id.

Tables

Example rows — the live problem includes the full dataset.

charging_sessions
station_idvehicle_idplug_inplug_outamount_paid
190012024-07-01 08:00:002024-07-01 10:30:005
190012024-07-02 11:00:002024-07-02 12:45:003
290012024-07-01 10:45:002024-07-01 12:00:006

Expected output

Your answer should return 2 rows with the columns vehicle_id, total_paid, avg_hourly_cost, top_station.

Starter code (SQL)

SELECT *
FROM charging_sessions;

Solve this SQL question free

Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related SQL questions