Hopper Company Queries III - Problem

You have three tables: Drivers, Rides, and AcceptedRides.

The Drivers table contains driver information including their join dates. The Rides table contains all ride requests (some may not be accepted). The AcceptedRides table contains information about rides that were actually completed.

Write a SQL query to compute the average_ride_distance and average_ride_duration for every 3-month window starting from January-March 2020 to October-December 2020.

Requirements:

  • Round both averages to 2 decimal places
  • The average is calculated by summing the total values from the three months and dividing by 3
  • Return results ordered by month (starting month number: 1=January, 2=February, etc.)
  • Include all 3-month windows even if no rides occurred

Table Schema

Drivers
Column Name Type Description
driver_id PK int Unique driver identifier
join_date date Date when driver joined Hopper company
Primary Key: driver_id
Rides
Column Name Type Description
ride_id PK int Unique ride identifier
user_id int 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 Reference to ride from Rides table
driver_id int 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 3-Month Windows
Input Tables:
Drivers
driver_id join_date
1 2019-12-10
2 2020-01-08
Rides
ride_id user_id requested_at
1 75 2020-01-01
2 54 2020-02-11
3 63 2020-03-04
4 99 2020-04-12
AcceptedRides
ride_id driver_id ride_distance ride_duration
1 1 20 30
2 2 40 40
3 1 35 45
Output:
month average_ride_distance average_ride_duration
1 31.67 38.33
2 31.67 38.33
3 25.00 28.33
4 25.00 28.33
5 11.67 15.00
6 11.67 15.00
7 0.00 0.00
8 0.00 0.00
9 0.00 0.00
10 0.00 0.00
💡 Note:

For January-March window: rides 1,2,3 with distances (20+40+35)/3 = 31.67. For February-April: rides 2,3,4 but ride 4 wasn't accepted, so (40+35+0)/3 = 25.00. Later windows have no rides so averages are 0.00.

Example 2 — No Accepted Rides
Input Tables:
Drivers
driver_id join_date
1 2020-01-01
Rides
ride_id user_id requested_at
1 75 2020-01-01
AcceptedRides
ride_id driver_id ride_distance ride_duration
Output:
month average_ride_distance average_ride_duration
1 0.00 0.00
2 0.00 0.00
3 0.00 0.00
4 0.00 0.00
5 0.00 0.00
6 0.00 0.00
7 0.00 0.00
8 0.00 0.00
9 0.00 0.00
10 0.00 0.00
💡 Note:

When no rides are accepted, all 3-month windows show 0.00 for both average distance and duration. The query still generates all required windows from month 1 to 10.

Constraints

  • 1 ≤ driver_id ≤ 300
  • 1 ≤ ride_id ≤ 1000
  • 1 ≤ user_id ≤ 500
  • 1 ≤ ride_distance ≤ 100
  • 1 ≤ ride_duration ≤ 120
  • All dates are in 2020
  • Each accepted ride exists in the Rides table

Visualization

Tap to expand
Hopper Company Queries III: 3-Month Rolling AveragesRides + AcceptedRidesride_idrequested_atdistance12020-01-012022020-02-114032020-03-04353-MonthWindowsOutput: Rolling Averagesmonthavg_distanceavg_duration131.6738.33231.6738.33325.0028.33Jan-Mar window: (20+40+35)/3 = 31.67Feb-Apr window: (40+35+0)/3 = 25.00
Understanding the Visualization
1
Input Tables
Drivers, Rides, and AcceptedRides
2
Group & Average
3-month rolling windows
3
Output
Monthly averages rounded to 2 decimals
Key Takeaway
🎯 Key Insight: Use CTEs to handle missing data and generate all required time windows
Asked in
Uber 15 Lyft 8
23.5K 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