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.
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
Visualization
Tap to expand
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!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code