cswork / libraryOpen practice workspace

Coding questions · Waymo

SQL: Monthly Ride Totals and Average Ratings, Newest Month First

Write a PostgreSQL query that reports, for each calendar month, the total number of rides and the average rating rounded to two decimals, newest month first. Tests month bucketing, counting unrated rides while averaging only rated ones, and ordering.

A ride service stores one row per ride. For every calendar month that has at least one ride, report the total number of rides and the average ride rating, with the most recent month first.

Input Table

rides: one row per ride.

ColumnTypeMeaning
ride_idINTEGERPrimary key
ride_dateDATEDate of the ride; never NULL
ride_ratingINTEGERRider's rating of the ride from 1 to 5; NULL if not rated
user_idINTEGERThe rider

Output Contract

  • Write one read-only PostgreSQL query (a single SELECT, optionally with CTEs).
  • Return one row per calendar month that has at least one ride, with columns:
    • ride_month (TEXT): the month formatted as YYYY-MM, for example 2024-06;
    • total_rides (BIGINT): the number of rides in that month, rated or not;
    • avg_rating (NUMERIC): the average of the non-NULL ratings of that month's rides, rounded to 2 decimal places; NULL if no ride in that month has a rating.
  • Order the result by ride_month descending.

Example

rides

ride_idride_dateride_ratinguser_id
12024-05-03510
22024-05-20411
32024-06-02NULL10
42024-06-15412
52024-06-30311
62024-06-11410
72024-07-01512

Expected result:

ride_monthtotal_ridesavg_rating
2024-0715.00
2024-0643.67
2024-0524.50

June has four rides; the unrated one counts toward total_rides but not toward the average, which is (4 + 3 + 4) / 3 = 3.666..., rounded to 3.67.

Constraints and Clarifications

  • Months are calendar months of ride_date; rides from the same month of different years are different months.
  • Months with no rides do not appear.
  • Ratings are integers, so compute the average in a non-integer type before rounding; integer division would truncate it.
  • ride_month is unique per row, so the ordering is total.

Hint: Two different denominators

The ride count and the rating average are computed over different sets of rows when some ratings are missing.

Monthly Ride Totals and Average Ratings, Newest Month First

Table rides

columntypeconstraints
ride_idINTEGERPRIMARY KEY
ride_dateDATENOT NULL
ride_ratingINTEGER
user_idINTEGER

Sample data:

ride_dateride_idride_ratinguser_id
2024-06-151107412
2023-12-171012NULL31
2025-01-011161526
2024-05-191098421
2023-06-301003432
2024-07-011119512
2024-10-311140123
2024-06-011105NULL10
2023-12-311015234
2024-09-221133NULL22
2024-12-201152324
2024-01-011021132
2025-01-151164NULL10
2024-05-311101521
2023-06-141001531
2024-12-081148325
2024-06-301110311
2024-01-091026235
2024-09-051130NULL22
2025-01-151166426
2023-12-011009333
2024-12-311155224
2024-06-111106410
2025-01-011160424
2024-05-031094521
2025-01-311170327

Expected output

ride_monthtotal_ridesavg_rating
2025-0154
2024-1232.67
2024-1011
2024-092NULL
2024-0715
2024-0643.67
2024-0534.67
2024-0121.5
2023-1232.5
2023-0624.5

Hints

  1. Two different denominators: the ride count and the rating average are computed over different sets of rows when some ratings are missing.