Skip to content

Average rating per city, weighted by trips

MediumFull bankGroup and aggregateNULL handlingJoins14 min2 hidden datasets

The task

The quality team publishes one rider rating per city. A rider may skip the rating, so rider_rating is NULL on many completed trips.

The city rating is the average of rider_rating across all rated completed trips that happened in the city (trips.city_id). Every rated trip counts once, so a driver with 200 rated trips weighs more than a driver with 2. Do not average the drivers' own averages.

Return one row for every city in cities:

  • city_name
  • completed_trips: the number of completed trips in the city, rated or not
  • rated_trips: how many of those have a rating
  • avg_rating: the city rating rounded to 2 decimal places, or NULL when the city has no rated trip

Sort by city_name ascending.

Expected output

city_namecompleted_tripsrated_tripsavg_rating
  • Sorted by city_name ascending.
  • Numbers rounded to 2 decimal places.
  • Checked on the sample and on 2 hidden datasets. How grading works

Tables Ride-hailing marketplace · click a name to insert it

cities4 rows in the sample

Cities we operate in. Key: city_id.

intUnique id.
textName.
textTwo-letter country code.
drivers60 rows in the sample

One row per driver. Key: driver_id.

intUnique id.
textName.
intHome city.
dateFirst day driving.
textstandard, xl or electric.
riders220 rows in the sample

One row per rider. Key: rider_id.

intUnique id.
textName.
dateSign-up day.
trips1,800 rows in the sample

One row per ride request. Key: trip_id.

intUnique id.
intWho asked.
intDriver who accepted. NULL when nobody did.
intWhere.
timestampRequest time (UTC).
timestampPickup time. NULL if never picked up.
timestampDrop-off time. NULL unless completed.
textcompleted, rider_cancelled, driver_cancelled or no_driver.
numeric(10,2)Fare in local currency units. NULL unless completed.
numeric(6,2)Trip distance. NULL unless completed.
intRider's rating of the driver, 1 to 5. NULL if not rated.

Related questions

Loading the editor and PostgreSQL