🤔 문제 분석
1) Return the result table ordered by travelled_distance in descending order,
2) if two or more users traveled the same distance, order them by their name in ascending order.
💡 풀이
SELECT U.name, if(R.distance IS NULL, 0, R.distance)AS travelled_distance
FROM Users AS U
LEFT JOIN (SELECT user_id, sum(distance) AS distance
FROM Rides
GROUP BY user_id)
AS R ON U.id = R.user_id
ORDER BY travelled_distance DESC, name
'코딩테스트 > 리트코드 SQL' 카테고리의 다른 글
[리트코드/SQL] 31. Actors and Directors Who Cooperated At Least Three Times (1050) (0) | 2023.02.14 |
---|---|
[리트코드/SQL] 30. Duplicate Emalis (182) (0) | 2022.12.23 |
[리트코드/SQL] 27. Capital Gain/Loss(1393) (0) | 2022.12.21 |
[리트코드/SQL] 26. Find Total Time Spent by Each Employee (1741) (0) | 2022.12.21 |
[리트코드/SQL] 25. The Latest Login in 2020 (1890) (0) | 2022.12.18 |
댓글