Table: Users
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| user_id | int |
| name | varchar |
+-------------+---------+
user_id is the column with unique values for this table.
Each row of this table contains user id and name.
Table: Rides
+--------------+------+ | Column Name | Type | +--------------+------+ | ride_id | int | | user_id | int | | distance | int | +--------------+------+ ride_id is the column of unique values for this table. Each row of this table contains ride id, user id, and traveled distance.
Write a solution to calculate the distance traveled by each user. If there is a user who hasn't completed any rides, then their distance should be considered as 0. Output the user_id, name and total traveled distance.
Return the result table ordered by user_id in ascending order.
The result format is in the following example.
Example 1:
Input: Users table: +---------+---------+ | user_id | name | +---------+---------+ | 17 | Addison | | 14 | Ethan | | 4 | Michael | | 2 | Avery | | 10 | Eleanor | +---------+---------+ Rides table: +---------+---------+----------+ | ride_id | user_id | distance | +---------+---------+----------+ | 72 | 17 | 160 | | 42 | 14 | 161 | | 45 | 4 | 59 | | 32 | 2 | 197 | | 15 | 4 | 357 | | 56 | 2 | 196 | | 10 | 14 | 25 | +---------+---------+----------+ Output: +---------+---------+-------------------+ | user_id | name | traveled distance | +---------+---------+-------------------+ | 2 | Avery | 393 | | 4 | Michael | 416 | | 10 | Eleanor | 0 | | 14 | Ethan | 186 | | 17 | Addison | 160 | +---------+---------+-------------------+ Explanation: - User id 2 completed two journeys of 197 and 196, resulting in a combined travel distance of 393. - User id 4 completed two journeys of 59 and 357, resulting in a combined travel distance of 416. - User id 14 completed two journeys of 161 and 25, resulting in a combined travel distance of 186. - User id 16 completed only one journey of 160. - User id 10 did not complete any journeys, thus the total travel distance remains at 0. Returning the table orderd by user_id in ascending order.
Problem Overview: You are given two tables: Users and Rides. Each ride stores the distance traveled by a user. The task is to compute the total traveled distance for every user, including users who never took a ride. The result must show each user's id, name, and the summed distance.
Approach 1: LEFT JOIN + GROUP BY SUM (O(U + R) time, O(1) extra space)
The core idea is to join the Users table with the Rides table and aggregate ride distances per user. Use a LEFT JOIN so every user appears in the result even if they have no rides. Then apply GROUP BY user_id and compute SUM(distance) to accumulate all rides for that user. When a user has no matching rides, SUM() returns NULL, so wrap it with COALESCE(..., 0) to output 0.
This approach works because relational databases efficiently process joins and aggregations during a single scan of the joined dataset. The database groups rows by user and calculates the sum during aggregation. Time complexity is O(U + R) where U is the number of users and R is the number of rides, since the engine scans both tables once during the join. Space complexity is O(1) outside the result set because aggregation happens internally.
This pattern appears frequently in SQL interview questions: join a dimension table with an activity table and compute totals per entity. Understanding how LEFT JOIN preserves unmatched rows is critical. Problems involving user activity, purchases, or metrics often rely on the same technique using SQL aggregation and joins.
Approach 2: Correlated Subquery Aggregation (O(U * R) worst case, O(1) space)
Another option is computing the total distance with a correlated subquery for each user. For every row in Users, run a subquery that calculates SUM(distance) from Rides filtered by user_id. This avoids an explicit join but repeatedly scans the rides table. In the worst case, the database performs the aggregation once per user, resulting in O(U * R) time.
While simpler to read, this approach performs worse on large datasets because the rides table may be scanned many times. Modern query optimizers sometimes rewrite it into a join internally, but relying on that behavior is not ideal.
Recommended for interviews: The LEFT JOIN + GROUP BY solution is the expected answer. It demonstrates a clear understanding of relational joins, aggregation, and handling missing rows with COALESCE. Interviewers want to see that you recognize the pattern of joining activity data and summarizing it using database aggregation.
We can use a left join to connect the two tables, and then use group by sum to calculate the total distance for each user. Note that if a user has not completed any rides, their distance should be considered as 0.
MySQL
| Approach | Time | Space | When to Use |
|---|---|---|---|
| LEFT JOIN + GROUP BY SUM | O(U + R) | O(1) | Standard SQL aggregation when summarizing activity per user |
| Correlated Subquery | O(U * R) worst case | O(1) | Simple queries or very small datasets where readability matters |
Leetcode 2837 - Total Traveled Distance - Solved by Everyday Data Science | JOIN, IFNULL() SUM() • Everyday Data Science • 612 views views
Practice Total Traveled Distance with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor