Hopper Company Queries II - Problem

You are given three tables for a ride-sharing company:

Drivers table contains information about drivers and when they joined the company.

Rides table contains all ride requests (both accepted and rejected).

AcceptedRides table contains details about rides that were actually accepted by drivers.

Write a SQL query to calculate the percentage of working drivers for each month of 2020, where:

  • Available drivers in a month = drivers who joined on or before that month
  • Working drivers in a month = available drivers who accepted at least one ride during that month
  • Working percentage = (working drivers / available drivers) × 100

If no drivers are available in a month, consider the working percentage to be 0.

Return results ordered by month (1=January, 2=February, etc.) and round the percentage to 2 decimal places.

Table Schema

Drivers
Column Name Type Description
driver_id PK int Unique driver identifier
join_date date Date when driver joined the company
Primary Key: driver_id
Rides
Column Name Type Description
ride_id PK int Unique ride identifier
user_id int ID of user who requested the ride
requested_at date Date when ride was requested
Primary Key: ride_id
AcceptedRides
Column Name Type Description
ride_id PK int Unique accepted ride identifier
driver_id int ID of driver who accepted the ride
ride_distance int Distance of the ride in units
ride_duration int Duration of the ride in minutes
Primary Key: ride_id

Input & Output

Example 1 — Basic Driver Activity
Input Tables:
Drivers
driver_id join_date
10 2019-12-10
8 2020-01-13
5 2020-02-16
7 2020-03-08
4 2020-05-17
1 2020-10-24
6 2021-01-05
Rides
ride_id user_id requested_at
6 75 2020-01-01
1 54 2020-02-11
10 63 2020-03-07
19 39 2020-04-30
3 41 2020-06-21
13 52 2020-07-20
7 69 2020-08-03
17 70 2020-08-17
20 81 2020-11-02
5 57 2020-11-09
2 42 2020-12-04
11 68 2020-12-17
AcceptedRides
ride_id driver_id ride_distance ride_duration
10 10 63 38
13 10 73 96
7 8 100 28
17 7 119 68
20 1 121 92
5 7 42 101
2 4 6 38
11 8 37 43
Output:
month working_percentage
1 0
2 0
3 25
4 0
5 0
6 0
7 20
8 40
9 0
10 0
11 33.33
12 33.33
💡 Note:

For January 2020: Available drivers = 2 (driver 10 joined in 2019, driver 8 joined in January). Working drivers = 1 (only driver 10 accepted ride 10). Percentage = 1/2 * 100 = 50.00%

For March 2020: Available drivers = 3 (drivers 10, 8, 5). Working drivers = 1 (only driver 10 accepted ride 10). Percentage = 1/3 * 100 = 33.33%

Example 2 — No Available Drivers
Input Tables:
Drivers
driver_id join_date
1 2021-01-01
Rides
ride_id user_id requested_at
1 50 2020-01-01
AcceptedRides
ride_id driver_id ride_distance ride_duration
Output:
month working_percentage
1 0.00
2 0.00
3 0.00
4 0.00
5 0.00
6 0.00
7 0.00
8 0.00
9 0.00
10 0.00
11 0.00
12 0.00
💡 Note:

Since the only driver joined in 2021, there are no available drivers in any month of 2020. The working percentage is 0.00% for all months.

Constraints

  • 1 ≤ driver_id ≤ 10^6
  • 1 ≤ ride_id ≤ 10^6
  • 1 ≤ user_id ≤ 10^6
  • join_date is a valid date
  • requested_at is a valid date
  • ride_distance ≥ 1
  • ride_duration ≥ 1

Visualization

Tap to expand
Hopper Company Queries II INPUT TABLES Drivers driver_id join_date 1 2019-12-15 2 2020-02-10 Rides ride_id requested_at 1, 2, 3... 2020-01-xx AcceptedRides ride_id driver_id 1 1 Year: 2020 (12 months) Track driver availability ALGORITHM STEPS 1 Generate Months Create sequence 1-12 WITH RECURSIVE months 2 Count Available Drivers Cumulative sum of drivers joined on/before month SUM(joined) OVER (ORDER BY month) 3 Count Working Drivers Distinct drivers with accepted rides in month COUNT(DISTINCT driver_id) 4 Calculate Percentage working/available * 100 ROUND(pct, 2) FINAL RESULT month working_pct 1 100.00 2 50.00 3 66.67 4 33.33 5 0.00 6 25.00 7 50.00 ... ... 11 75.00 12 100.00 ORDER BY month OK - 12 rows Sorted by month Key Insight: Use RECURSIVE CTE to generate all 12 months, then LEFT JOIN to ensure months with 0 drivers appear. Available drivers accumulate over time (cumulative sum), while working drivers are counted per month. Handle division by zero with IFNULL/COALESCE when no drivers are available in early months. TutorialsPoint - Hopper Company Queries II | Optimal Solution
Asked in
Uber 15 Lyft 8
23.4K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen