Total Traveled Distance - Problem

You're building a ride-sharing analytics dashboard and need to calculate the total distance traveled by each user in your system.

You have two tables:

  • Users table: Contains user information (user_id, name)
  • Rides table: Contains ride records (ride_id, user_id, distance)

Your task is to write a SQL query that returns each user's user_id, name, and total_traveled_distance. Here's the catch: some users might not have taken any rides yet, so their total distance should be 0.

The results should be ordered by user_id in ascending order.

Example:

If Alice (user_id=1) took 3 rides covering 10, 15, and 20 miles, her total should be 45 miles. If Bob (user_id=2) hasn't taken any rides, his total should be 0.

Input & Output

example_1.sql โ€” Basic Case
$ Input: Users: +----------+-------+ | user_id | name | +----------+-------+ | 1 | Alice | | 2 | Bob | | 3 | Alex | +----------+-------+ Rides: +----------+----------+----------+ | ride_id | user_id | distance | +----------+----------+----------+ | 1 | 1 | 120 | | 2 | 2 | 317 | | 3 | 3 | 222 | | 4 | 1 | 44 | | 5 | 2 | 1 | +----------+----------+----------+
โ€บ Output: +----------+-------+------------------------+ | user_id | name | total_traveled_distance| +----------+-------+------------------------+ | 1 | Alice | 164 | | 2 | Bob | 318 | | 3 | Alex | 222 | +----------+-------+------------------------+
๐Ÿ’ก Note: Alice (user_id=1) has 2 rides: 120 + 44 = 164 miles. Bob (user_id=2) has 2 rides: 317 + 1 = 318 miles. Alex (user_id=3) has 1 ride: 222 miles.
example_2.sql โ€” User with No Rides
$ Input: Users: +----------+--------+ | user_id | name | +----------+--------+ | 1 | Alice | | 2 | Bob | | 3 | Charlie| +----------+--------+ Rides: +----------+----------+----------+ | ride_id | user_id | distance | +----------+----------+----------+ | 1 | 1 | 100 | | 2 | 1 | 200 | +----------+----------+----------+
โ€บ Output: +----------+--------+------------------------+ | user_id | name | total_traveled_distance| +----------+--------+------------------------+ | 1 | Alice | 300 | | 2 | Bob | 0 | | 3 | Charlie| 0 | +----------+--------+------------------------+
๐Ÿ’ก Note: Alice has traveled 300 miles total, while Bob and Charlie haven't taken any rides yet, so their distances are 0.
example_3.sql โ€” Single User Case
$ Input: Users: +----------+------+ | user_id | name | +----------+------+ | 1 | John | +----------+------+ Rides: +----------+----------+----------+ | ride_id | user_id | distance | +----------+----------+----------+ | 1 | 1 | 50 | +----------+----------+----------+
โ€บ Output: +----------+------+------------------------+ | user_id | name | total_traveled_distance| +----------+------+------------------------+ | 1 | John | 50 | +----------+------+------------------------+
๐Ÿ’ก Note: Edge case with single user who has taken one ride of 50 miles.

Visualization

Tap to expand
๐Ÿ‘ฅ Users1 | Alice2 | Bob3 | Charlie4 | Diana๐Ÿš— RidesR1: User1, 120miR2: User1, 44miR3: User3, 200miR4: User4, 50mi๐Ÿ“Š Final Result1 | Alice | 164mi2 | Bob | 0mi3 | Charlie | 200mi4 | Diana | 50miโœ“ All users included!LEFT JOINGROUP BY+ SUM๐ŸŽฏ Key Insight: LEFT JOIN preserves ALL usersEven users without rides (like Bob) appear with 0 distanceCOALESCE(SUM(distance), 0) handles NULL values elegantly
Understanding the Visualization
1
Start with All Users
LEFT JOIN ensures every user from Users table appears in results
2
Match Ride Data
Find corresponding rides for each user (NULL if no rides)
3
Group and Sum
GROUP BY user to aggregate multiple rides per user
4
Handle Missing Data
COALESCE converts NULL sums to 0 for users with no rides
Key Takeaway
๐ŸŽฏ Key Insight: LEFT JOIN is perfect for "show all users, even inactive ones" scenarios. It's the SQL equivalent of a comprehensive report that doesn't leave anyone out!

Time & Space Complexity

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

Single pass through users (n) and rides (m) tables, plus sorting

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Space for the result set containing all users

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค Users.user_id โ‰ค 106
  • 1 โ‰ค Rides.ride_id โ‰ค 106
  • 1 โ‰ค Rides.distance โ‰ค 1000
  • All user_id values in Users table are unique
  • All ride_id values in Rides table are unique
  • user_id in Rides table references valid users in Users table
Asked in
Uber 45 Lyft 32 DoorDash 28 Amazon 25
89.4K Views
High Frequency
~15 min Avg. Time
1.8K 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