EV Charging Spend per Vehicle
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.
| station_id | vehicle_id | plug_in | plug_out | amount_paid |
|---|---|---|---|---|
| 1 | 9001 | 2024-07-01 08:00:00 | 2024-07-01 10:30:00 | 5 |
| 1 | 9001 | 2024-07-02 11:00:00 | 2024-07-02 12:45:00 | 3 |
| 2 | 9001 | 2024-07-01 10:45:00 | 2024-07-01 12:00:00 | 6 |
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