Average Driver Rating per City for Completed Trips

Quick Overview

Write a SQL query that returns the average driver rating of completed trips for each city with more than one completed trip. It tests filtering before aggregation, HAVING thresholds, rounding to two decimals and a sort order with tiebreakers.

Average Driver Rating per City for Completed Trips

Company: Uber

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

For each city, compute the average `driver_rating` over its completed trips, rounded to 2 decimal places. Return only cities with more than one completed trip. ```hint Which rows are left when you count Check which trips should still be in play at the moment you count trips per city, and compare that with the rows the average uses. ``` ### Input Table(s) `cities` | Column | Type | Meaning | |---|---|---| | city_id | INT | Unique city identifier | | city_name | VARCHAR(100) | City name, never NULL, not guaranteed unique | Primary key: `city_id`. `trips` | Column | Type | Meaning | |---|---|---| | trip_id | INT | Unique trip identifier | | city_id | INT | City where the trip took place | | driver_id | INT | Driver of the trip, never NULL | | status | VARCHAR(20) | `'completed'`, `'cancelled_by_driver'` or `'cancelled_by_rider'` | | driver_rating | DECIMAL(3,2) | Rating from 1.00 to 5.00, NULL when the trip was not completed | Primary key: `trip_id`. Foreign key: `trips.city_id` references `cities.city_id`. ### Output Contract - Columns, in this order: `city_name`, `avg_driver_rating`. - `avg_driver_rating` is the mean `driver_rating` of the city's completed trips, rounded to 2 decimal places. - One row per city with at least two completed trips. - Sort by `avg_driver_rating` descending (the rounded value), then `city_name` ascending, then `city_id` ascending. ### Example Input `cities`: | city_id | city_name | |---|---| | 1 | San Francisco | | 2 | Chicago | | 3 | Austin | | 4 | Denver | Input `trips`: | trip_id | city_id | driver_id | status | driver_rating | |---|---|---|---|---| | 1 | 1 | 501 | completed | 4.80 | | 2 | 1 | 502 | completed | 4.60 | | 3 | 1 | 503 | cancelled_by_rider | NULL | | 4 | 2 | 504 | completed | 4.90 | | 5 | 2 | 505 | completed | 4.50 | | 6 | 2 | 506 | completed | 4.70 | | 7 | 3 | 507 | completed | 5.00 | | 8 | 3 | 508 | cancelled_by_driver | NULL | | 9 | 4 | 509 | completed | 4.00 | | 10 | 4 | 510 | completed | 4.33 | | 11 | 4 | 511 | completed | 4.50 | Expected output: | city_name | avg_driver_rating | |---|---| | Chicago | 4.70 | | San Francisco | 4.70 | | Denver | 4.28 | San Francisco averages (4.80 + 4.60) / 2 = 4.70 and Chicago averages 14.10 / 3 = 4.70, so the tie goes to Chicago by name. Denver averages 12.83 / 3 = 4.2766..., rounded to 4.28. Austin has two trips but only one is completed, so it is excluded. ### Constraints and Clarifications - `trips` has at most 100,000 rows and `cities` at most 100 rows. - Only trips with `status = 'completed'` count, both for the average and for the "more than one trip" threshold. Cancelled trips are ignored entirely. - Every completed trip has a rating, and every cancelled trip has a NULL rating. - Two cities can share a name. They are separate cities with separate rows, and the final `city_id` sort key orders them. - The sort uses the rounded average, so two cities that both show 4.70 are ordered by name.

Overview: Write a SQL query that returns the average driver rating of completed trips for each city with more than one completed trip. It tests filtering before aggregation, HAVING thresholds, rounding to two decimals and a sort order with tiebreakers.

Uber logo
Uber
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

For each city, compute the average driver_rating over its completed trips, rounded to 2 decimal places. Return only cities with more than one completed trip.

Input Table(s)

cities

ColumnTypeMeaning
city_idINTUnique city identifier
city_nameVARCHAR(100)City name, never NULL, not guaranteed unique

Primary key: city_id.

trips

ColumnTypeMeaning
trip_idINTUnique trip identifier
city_idINTCity where the trip took place
driver_idINTDriver of the trip, never NULL
statusVARCHAR(20)'completed', 'cancelled_by_driver' or 'cancelled_by_rider'
driver_ratingDECIMAL(3,2)Rating from 1.00 to 5.00, NULL when the trip was not completed

Primary key: trip_id. Foreign key: trips.city_id references cities.city_id.

Output Contract

  • Columns, in this order: city_name , avg_driver_rating .
  • avg_driver_rating is the mean driver_rating of the city's completed trips, rounded to 2 decimal places.
  • One row per city with at least two completed trips.
  • Sort by avg_driver_rating descending (the rounded value), then city_name ascending, then city_id ascending.

Example

Input cities:

city_idcity_name
1San Francisco
2Chicago
3Austin
4Denver

Input trips:

trip_idcity_iddriver_idstatusdriver_rating
11501completed4.80
21502completed4.60
31503cancelled_by_riderNULL
42504completed4.90
52505completed4.50
62506completed4.70
73507completed5.00
83508cancelled_by_driverNULL
94509completed4.00
104510completed4.33
114511completed4.50

Expected output:

city_nameavg_driver_rating
Chicago4.70
San Francisco4.70
Denver4.28

San Francisco averages (4.80 + 4.60) / 2 = 4.70 and Chicago averages 14.10 / 3 = 4.70, so the tie goes to Chicago by name. Denver averages 12.83 / 3 = 4.2766..., rounded to 4.28. Austin has two trips but only one is completed, so it is excluded.

Constraints and Clarifications

  • trips has at most 100,000 rows and cities at most 100 rows.
  • Only trips with status = 'completed' count, both for the average and for the "more than one trip" threshold. Cancelled trips are ignored entirely.
  • Every completed trip has a rating, and every cancelled trip has a NULL rating.
  • Two cities can share a name. They are separate cities with separate rows, and the final city_id sort key orders them.
  • The sort uses the rounded average, so two cities that both show 4.70 are ordered by name.