How many drivers in the same city rate higher
The task
Driver operations wants each driver's standing in their home city: how many other drivers there have a better average rating.
Rules:
- A driver's rating is the average of
rider_ratingover theircompletedtrips that were rated (rider_ratingis not NULL). Unrated trips are ignored. - Only drivers with at least 10 rated completed trips take part, both as the driver being described and as someone being compared against.
- A driver's city is
drivers.city_id, not the city of the trip. higher_rated_driverscounts the participating drivers in the same city whose exact average is strictly higher. Drivers with equal averages do not count against each other.
Return driver_id, city_id, avg_rating (the driver's average, rounded to 2 decimal places, for display only) and higher_rated_drivers. Sort by city_id, then higher_rated_drivers, then driver_id.
Expected output
driver_idcity_idavg_ratinghigher_rated_drivers- Sorted by city_id ascending, then higher_rated_drivers ascending, then driver_id ascending.
- Numbers rounded to 2 decimal places.
- Checked on the sample and on 3 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
- Trips between a rider and a driver they already knowmedium
- Colleagues hired in the same month and departmenteasy
- Employees paid more than their managermedium
- Products bought together in the same ordermedium
- Raises of 9.5 percent or more over the previous salaryhard
- Repeat tickets from the same customer within a weekeasy
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL