Skip to content

Commit 094ce75

Browse files
committed
Upload 156
1 parent 152dc54 commit 094ce75

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ The problems and both submitted and site solutions are documented in individual
189189
| 153 | [Find Total Time Spent by Each Employee](https://leetcode.com/problems/find-total-time-spent-by-each-employee/description/) | [Solution](solutions/153_find_total_time_spent_by_each_employee.md) | LeetCode | Easy | |
190190
| 154 | [Find Users With Valid E-Mails](https://leetcode.com/problems/find-users-with-valid-e-mails/description/) | [Solution](solutions/154_find_users_with_valid_e_mails.md) | LeetCode | Easy | Regex |
191191
| 155 | [Actors and Directors Who Cooperated At Least Three Times](https://leetcode.com/problems/actors-and-directors-who-cooperated-at-least-three-times/description/) | [Solution](solutions/155_actors_and_directors_who_cooperated_at_least_three_times.md) | LeetCode | Easy | |
192+
| 156 | [Top Travellers](https://leetcode.com/problems/top-travellers/description/) | [Solution](solutions/156_top_travellers.md) | LeetCode | Easy | |
192193
<!-- Index End - WARNING: Do not delete or modify this markdown comment. -->
193194
<!--- cSpell:enable --->
194195

solutions/156_top_travellers.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# SQL Everyday \#156
2+
3+
## Top Travellers
4+
5+
Site: LeetCode\
6+
Difficulty per Site: Easy
7+
8+
## Problem
9+
10+
Write a solution to report the distance traveled by each user.
11+
12+
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*. [[Full Description](https://leetcode.com/problems/top-travellers/description/)]
13+
14+
## Submitted Solution
15+
16+
```sql
17+
-- Submitted Solution
18+
WITH cte AS (
19+
SELECT
20+
user_id
21+
,SUM(distance) AS sum_distrance
22+
FROM rides
23+
GROUP BY user_id
24+
)
25+
SELECT
26+
u.name
27+
,COALESCE(c.sum_distrance, 0) AS travelled_distance
28+
FROM Users AS u
29+
LEFT JOIN cte AS c ON u.id = c.user_id
30+
ORDER BY travelled_distance DESC, u.name ASC
31+
;
32+
```
33+
34+
## Site Solution
35+
36+
```sql
37+
-- LeetCode Solution
38+
SELECT
39+
u.name,
40+
IFNULL(SUM(distance),0) AS travelled_distance
41+
FROM
42+
Users u
43+
LEFT JOIN
44+
Rides r
45+
ON
46+
u.id = r.user_id
47+
GROUP BY
48+
u.id
49+
ORDER BY 2 DESC, 1 ASC
50+
```
51+
52+
## Notes
53+
54+
TBD
55+
56+
## NB
57+
58+
TBD
59+
60+
Go to [Index](../?tab=readme-ov-file#index)\
61+
Go to [Overview](../?tab=readme-ov-file)

0 commit comments

Comments
 (0)