Top Three Earning Drivers per City Each Week
Last updated: November 29, 2026
Quick Overview
Given a trips table, return the top three drivers by total completed-trip earnings in each city for each week, keeping ties.
Uber
November 29, 20260
0
0 solved
Given a trips table, return the top three drivers by total completed-trip earnings in each city for each week, keeping ties.
Driver earnings by city and week is a bread-and-butter Uber analytics query used in ops reviews and incentive targeting. The interviewer checks aggregation before ranking, the right ranking function for ties, and filtering on trip status.
What the Interviewer Expects
- Filter to completed trips before aggregating
- Aggregate to driver-city-week before applying the window
- Choose DENSE_RANK, RANK or ROW_NUMBER deliberately
- Partition the window by city and week
- Explain how ties change the output size
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you return exactly three rows per city-week?
- How would you include tips and incentives stored in other tables?
- How would you handle drivers who operate in two cities in the same week?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
trips(trip_id, driver_id, city_id, completed_at, fare_usd, status). Ask: is fare_usd the driver's earnings or the rider's total (they differ by service fee)? Which time zone defines the week (use ...
Approach
- Filter
status = 'completed'. - Aggregate per city, week and driver.
- Rank within each (city, week) by earnings descending with
DENSE_RANK, so tied drivers share a rank. - Keep rank at most ...