Skip to content

Commit 6fa6778

Browse files
committed
task: #1407
1 parent b0cd9c2 commit 6fa6778

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Have a good contributing!
4747
- [619. Biggest Single Number](./leetcode/easy/619.%20Biggest%20Single%20Number.sql)
4848
- [620. Not Boring Movies](./leetcode/easy/620.%20Not%20Boring%20Movies.sql)
4949
- [1068. Product Sales Analysis I](./leetcode/easy/1068.%20Product%20Sales%20Analysis%20I.sql)
50+
- [1407. Top Travellers](./leetcode/easy/1407.%20Top%20Travellers.sql)
5051
2. [Medium](./leetcode/medium/)
5152
- [176. Second Highest Salary](./leetcode/medium/176.%20Second%20Highest%20Salary.sql)
5253
- [184. Department Highest Salary](./leetcode/medium/184.%20Department%20Highest%20Salary.sql)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Question 1407. Top Travellers
3+
Link: https://leetcode.com/problems/top-travellers/description/
4+
5+
Table: Users
6+
7+
+---------------+---------+
8+
| Column Name | Type |
9+
+---------------+---------+
10+
| id | int |
11+
| name | varchar |
12+
+---------------+---------+
13+
id is the column with unique values for this table.
14+
name is the name of the user.
15+
16+
17+
Table: Rides
18+
19+
+---------------+---------+
20+
| Column Name | Type |
21+
+---------------+---------+
22+
| id | int |
23+
| user_id | int |
24+
| distance | int |
25+
+---------------+---------+
26+
id is the column with unique values for this table.
27+
user_id is the id of the user who traveled the distance "distance".
28+
29+
30+
Write a solution to report the distance traveled by each user.
31+
32+
Return the result table ordered by travelled_distance in descending order, if two or more users traveled the same distance, order them by their name in ascending order.
33+
*/
34+
35+
SELECT
36+
u.name,
37+
COALESCE(SUM(r.distance), 0) AS travelled_distance
38+
FROM Rides AS r
39+
RIGHT JOIN -- noqa: CV08
40+
Users AS u
41+
ON r.user_id = u.id
42+
GROUP BY u.id, u.name
43+
ORDER BY travelled_distance DESC, u.name ASC

0 commit comments

Comments
 (0)