Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Have a good contributing!
- [619. Biggest Single Number](./leetcode/easy/619.%20Biggest%20Single%20Number.sql)
- [620. Not Boring Movies](./leetcode/easy/620.%20Not%20Boring%20Movies.sql)
- [1068. Product Sales Analysis I](./leetcode/easy/1068.%20Product%20Sales%20Analysis%20I.sql)
- [1407. Top Travellers](./leetcode/easy/1407.%20Top%20Travellers.sql)
2. [Medium](./leetcode/medium/)
- [176. Second Highest Salary](./leetcode/medium/176.%20Second%20Highest%20Salary.sql)
- [184. Department Highest Salary](./leetcode/medium/184.%20Department%20Highest%20Salary.sql)
Expand Down
43 changes: 43 additions & 0 deletions leetcode/easy/1407. Top Travellers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Question 1407. Top Travellers
Link: https://leetcode.com/problems/top-travellers/description/

Table: Users

+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| name | varchar |
+---------------+---------+
id is the column with unique values for this table.
name is the name of the user.


Table: Rides

+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| user_id | int |
| distance | int |
+---------------+---------+
id is the column with unique values for this table.
user_id is the id of the user who traveled the distance "distance".


Write a solution to report the distance traveled by each user.

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.
*/

SELECT
u.name,
COALESCE(SUM(r.distance), 0) AS travelled_distance
FROM Rides AS r
RIGHT JOIN -- noqa: CV08
Users AS u
ON r.user_id = u.id
GROUP BY u.id, u.name
ORDER BY travelled_distance DESC, u.name ASC