How do you optimize a slow-running SQL query that processes millions of rows for Uber?
Treat this as a practical PostgreSQL exercise. Use the provided ride, driver, and city data to return the requested result while explaining how the query would scale.
Output
- One row per qualifying active driver and city, with
city_name, driver_id, driver_name, completed_rides, avg_fare, and driver_rank.
- Include completed rides from
2025-01-01 through 2025-03-31, excluding inactive or unknown drivers and cities.
- Rank drivers within each city by completed rides descending, then average fare descending, and return the top three per city. Order by
city_name, driver_rank, and driver_id.