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 25 28.33
3 11.67 15
4 0 0
5 0 0
6 0 0
7 0 0
8 0 0
9 0 0
10 0 0
💡 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 INPUT TABLES Rides Table ride_id | driver_id ride_distance | ride_duration requested_at 1 | 101 | 15.5 | 30 | 2020-01-15 2 | 102 | 22.0 | 45 | 2020-02-20 AcceptedRides ride_id | driver_id accept_date 1 | 101 | 2020-01-15 3-Month Windows (2020) Jan-Mar | Feb-Apr | Mar-May Apr-Jun | May-Jul | Jun-Aug Jul-Sep | Aug-Oct | Sep-Nov Oct-Dec ALGORITHM STEPS 1 Generate Months Create sequence 1-12 for 2020 2 Join Rides Data Link Rides with AcceptedRides 3 Window Aggregation Sum for each 3-month window 4 Calculate Averages Divide by 3, round to 2 decimals Window Calculation Jan Feb Mar Apr ... Window 1 AVG = SUM(dist) / 3 FINAL RESULT mo avg_dist avg_dur 1 12.50 25.00 2 14.33 28.67 3 15.00 30.00 4 16.25 32.50 5 18.00 36.00 6 19.50 39.00 7 21.00 42.00 8 22.33 44.67 9 24.00 48.00 10 25.50 51.00 ... ... ... OK - 10 rows Ordered by month Key Insight: Use a RECURSIVE CTE to generate months 1-12, then LEFT JOIN with rides data filtered to 2020. For each starting month M, aggregate rides from months M, M+1, M+2 using BETWEEN condition. Calculate averages by dividing totals by 3 and use ROUND(value, 2) for precision. Filter months 1-10 only. TutorialsPoint - Hopper Company Queries III | Optimal Solution
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