Top Travellers - Problem
Top Travellers

You work for a ride-sharing company and need to generate a leaderboard showing which users have traveled the most distance. You have access to two database tables:

Users Table:
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| name | varchar |
+---------------+---------+


Rides Table:
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| user_id | int |
| distance | int |
+---------------+---------+


Your task is to write an SQL query that calculates the total distance traveled by each user and returns the results sorted by:
1. Total distance in descending order (highest first)
2. User name in ascending order (alphabetical) for ties

The result should include all users, even those who haven't taken any rides (they should show 0 distance).

Input & Output

example_1.sql โ€” Basic Example
$ Input: Users table: +------+-----------+ | id | name | +------+-----------+ | 1 | Alice | | 2 | Bob | | 3 | Alex | | 4 | Donald | +------+-----------+ Rides table: +------+---------+----------+ | id | user_id | distance | +------+---------+----------+ | 1 | 1 | 120 | | 2 | 2 | 317 | | 3 | 3 | 222 | | 4 | 7 | 100 | | 5 | 3 | 30 | +------+---------+----------+
โ€บ Output: +-----------+-------------------+ | name | travelled_distance| +-----------+-------------------+ | Bob | 317 | | Alex | 252 | | Alice | 120 | | Donald | 0 | +-----------+-------------------+
๐Ÿ’ก Note: Bob has the highest distance (317), followed by Alex (222+30=252), Alice (120), and Donald who has no rides (0). Note that user_id 7 in rides doesn't exist in users table, so it's ignored.
example_2.sql โ€” Tie Breaking
$ Input: Users table: +------+-----------+ | id | name | +------+-----------+ | 1 | Alice | | 2 | Bob | | 3 | Carol | +------+-----------+ Rides table: +------+---------+----------+ | id | user_id | distance | +------+---------+----------+ | 1 | 1 | 100 | | 2 | 2 | 100 | +------+---------+----------+
โ€บ Output: +-----------+-------------------+ | name | travelled_distance| +-----------+-------------------+ | Alice | 100 | | Bob | 100 | | Carol | 0 | +-----------+-------------------+
๐Ÿ’ก Note: Alice and Bob both traveled 100 distance, so they're sorted alphabetically. Carol has no rides and appears last with 0 distance.
example_3.sql โ€” No Rides Edge Case
$ Input: Users table: +------+-----------+ | id | name | +------+-----------+ | 1 | Alice | | 2 | Bob | +------+-----------+ Rides table: +------+---------+----------+ | id | user_id | distance | +------+---------+----------+ (empty table)
โ€บ Output: +-----------+-------------------+ | name | travelled_distance| +-----------+-------------------+ | Alice | 0 | | Bob | 0 | +-----------+-------------------+
๐Ÿ’ก Note: When no rides exist, all users should still appear in the result with 0 distance, sorted alphabetically by name.

Visualization

Tap to expand
๐Ÿ‘ฅ MembersAliceBobCarol๐Ÿš— TripsAlice: 100miAlice: 200miBob: 50miLEFT JOIN๐Ÿ”„ ProcessingAlice: 100+200=300Bob: 50Carol: 0 (no trips)Sort by distance โ†“๐Ÿ† Leaderboard1Alice (300mi)2Bob (50mi)3Carol (0mi)๐Ÿ’ก Key: LEFT JOIN ensures ALL members appear, even those with zero trips!
Understanding the Visualization
1
Member Database
Start with all registered members (Users table)
2
Trip Records
Have separate records of all trips taken (Rides table)
3
Smart Matching
Use LEFT JOIN to match members with their trips (keeps members with no trips)
4
Calculate Totals
Sum up distances for each member, using 0 for those with no trips
5
Create Leaderboard
Sort by total distance (highest first), break ties alphabetically
Key Takeaway
๐ŸŽฏ Key Insight: LEFT JOIN is crucial to include all users, while GROUP BY with COALESCE elegantly handles aggregation and null values in one efficient query.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n + m + k log k)

O(n + m) for JOIN where n=users, m=rides, plus O(k log k) for sorting k results

n
2n
โšก Linearithmic
Space Complexity
O(k)

Space for k result rows where k is number of users

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค Users.id โ‰ค 500
  • 1 โ‰ค Users.name.length โ‰ค 30
  • 1 โ‰ค Rides.id โ‰ค 500
  • 1 โ‰ค Rides.user_id โ‰ค 500
  • 1 โ‰ค Rides.distance โ‰ค 1000
  • All user names are unique
  • Some users may have no rides
Asked in
Uber 45 Lyft 35 Amazon 30 Google 25
28.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen