Daily Toll Revenue by Vehicle Type

SQL coding challenge · Difficulty: medium · Topic: Aggregation · +150 XP

Problem

You are on the team that runs Uber's toll reconciliation. Every time a vehicle passes a toll plaza, one row lands in toll_log.

The plaza charges a different rate for each vehicle type, and that rate is higher during peak hours.

Report the total toll revenue collected per day.

Tables

Table: toll_log

| Column | Type | Description |
| --- | --- | --- |
| toll_id | INT | Unique toll transaction id |
| vehicle_id | VARCHAR | Vehicle identifier |
| vehicle_type | VARCHAR | One of `car`, `bike`, `truck`, `bus` |
| toll_time | DATETIME | Date and time of the crossing |

Pricing

| Vehicle type | Peak | Off-peak |
| --- | --- | --- |
| car | 150 | 100 |
| bike | 80 | 50 |
| truck | 250 | 180 |
| bus | 200 | 140 |

Peak hours are 08:00:00-09:59:59 and 17:00:00-19:59:59. Every other

time is off-peak.

Read those windows carefully. 08:00:00 is peak but 10:00:00 is not;

17:00:00 is peak but 20:00:00 is not. A crossing counts as peak when its

hour is 8, 9, 17, 18 or 19.

Example Input

| toll_id | vehicle_id | vehicle_type | toll_time |
| --- | --- | --- | --- |
| 1 | V101 | car | 2024-01-15 07:30:00 |
| 2 | V102 | car | 2024-01-15 08:30:00 |
| 3 | V103 | bike | 2024-01-15 09:15:00 |
| 4 | V104 | truck | 2024-01-15 18:00:00 |
| 5 | V105 | car | 2024-01-15 21:00:00 |
| 6 | V106 | bus | 2024-01-16 08:45:00 |
| 7 | V107 | bike | 2024-01-16 12:30:00 |
| 8 | V108 | truck | 2024-01-16 19:30:00 |

Expected Output

| toll_date | total_revenue |
| --- | --- |
| 2024-01-15 | 680 |
| 2024-01-16 | 500 |

Explanation

2024-01-1507:30 car is off-peak (100), 08:30 car is peak (150),

09:15 bike is peak (80), 18:00 truck is peak (250), 21:00 car is off-peak

(100). Total 100 + 150 + 80 + 250 + 100 = 680.

2024-01-1608:45 bus is peak (200), 12:30 bike is off-peak (50),

19:30 truck is peak (250). Total 200 + 50 + 250 = 500.

Notes

What this SQL challenge teaches you

“Daily Toll Revenue by Vehicle Type” is a medium-level SQL challenge focused on Aggregation. Working through it gives you hands-on practice with CASE WHEN, conditional aggregation, GROUP BY, DATE, HOUR, peak pricing — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.

Concepts covered

How to approach it

If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:

  1. Two things vary at once: the vehicle type and whether the crossing was in a peak window. Work out the rate for a SINGLE row first, then wrap the whole thing in SUM().
  2. `HOUR(toll_time)` gives you 0-23. Peak is exactly the set {8, 9, 17, 18, 19}, so `HOUR(toll_time) IN (8,9,17,18,19)` is the whole peak test -- no BETWEEN on timestamps needed.
  3. Nest the CASEs: the outer one asks peak or off-peak, and each branch is an inner `CASE vehicle_type` returning that column of the price table. Then `SUM(...)` and `GROUP BY DATE(toll_time)`.
  4. Careful with the boundaries. `10:00:00` and `20:00:00` are OFF-peak -- a condition like `HOUR(toll_time) BETWEEN 8 AND 10` charges 10am at the peak rate and will fail the hidden tests.

Where this comes up

Variations of this problem have been reported in interviews at Uber. Interviewers use it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.

How to practise it on PySpark.in

Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.

Related SQL challenges

Frequently asked questions

Do I need to install Spark or a database to solve this?

No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.

Is this challenge free?

Yes - the problem, the sample dataset, the hints and unlimited test runs are free.

What level is it?

It is rated medium and covers Aggregation.

Solve this challenge free on PySpark.in