Site: LeetCode
Difficulty per Site: Easy
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. [Full Description]
-- Submitted Solution
SELECT
u.user_id
,u.name
,COALESCE(SUM(r.distance), 0) AS "traveled distance"
FROM Users AS u
LEFT JOIN Rides AS r ON u.user_id = r.user_id
GROUP BY u.user_id, u.name
ORDER BY u.user_id ASC
;
-- LeetCode Solution
SELECT u.user_id,
u.name,
IFNULL(SUM(distance), 0) AS 'traveled distance'
FROM Users AS u
LEFT JOIN Rides AS r
ON u.user_id = r.user_id
GROUP BY user_id, name
ORDER BY user_id
TBD
TBD