Skip to content

Driver rating leaderboard, unrated drivers last

MediumFull bankRanking and top NNULL handling15 min2 hidden datasets

The task

The operations team of the pilot city (the city with city_id 1) wants a leaderboard of its drivers: every driver whose home city (drivers.city_id) is 1.

For each driver, look at their completed trips that have a rider_rating (not NULL). The driver's avg_rating is the average of those ratings, rounded to 2 decimal places for display, and rated_trips is how many rated trips they have. A driver with no rated trips has an avg_rating of NULL and a rated_trips of 0, and is still on the leaderboard.

Rank drivers with RANK by exact average rating (highest first), and then by rated_trips (most first), so a larger sample wins over a smaller one at the same average. Drivers that are equal on both share a rank, and the next rank skips ahead. Drivers without a rating come after every rated driver and share the last rank.

Return full_name, rated_trips, avg_rating and rating_rank. Sort by rating_rank ascending, then full_name ascending.

Expected output

full_namerated_tripsavg_ratingrating_rank
  • Sorted by rating_rank ascending, then full_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