Driver rating leaderboard, unrated drivers last
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.
| int | Unique id. | |
| text | Name. | |
| text | Two-letter country code. |
drivers60 rows in the sample
One row per driver. Key: driver_id.
| int | Unique id. | |
| text | Name. | |
| int | Home city. | |
| date | First day driving. | |
| text | standard, xl or electric. |
riders220 rows in the sample
One row per rider. Key: rider_id.
| int | Unique id. | |
| text | Name. | |
| date | Sign-up day. |
trips1,800 rows in the sample
One row per ride request. Key: trip_id.
| int | Unique id. | |
| int | Who asked. | |
| int | Driver who accepted. NULL when nobody did. | |
| int | Where. | |
| timestamp | Request time (UTC). | |
| timestamp | Pickup time. NULL if never picked up. | |
| timestamp | Drop-off time. NULL unless completed. | |
| text | completed, 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. | |
| int | Rider's rating of the driver, 1 to 5. NULL if not rated. |
Related questions
- Average first purchase value by campaign, organic includedmedium
- The third longest completed trip in each citymedium
- Three slowest first responses in each channeleasy
- Top three drivers in each city, ties at third place keptmedium
- Best seller on each sales channel in the first quartermedium
- Each customer's largest single expense, with tie-breakersmedium
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL