Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 422aad9

Browse files
add 1407
1 parent 2cb5d25 commit 422aad9

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,7 @@ _If you like this project, please leave me a star._ ★
940940

941941
| # | Title | Solutions | Video | Difficulty | Tag
942942
|-----|----------------|---------------|---------------|---------------|-------------
943+
|1407|[Top Travellers](https://leetcode.com/problems/top-travellers/)|[Solution](../master/database/_1407.sql) || Easy |
943944
|1384|[Total Sales Amount by Year](https://leetcode.com/problems/total-sales-amount-by-year/)|[Solution](../master/database/_1384.sql) || Hard |
944945
|1378|[Replace Employee ID With The Unique Identifier](https://leetcode.com/problems/replace-employee-id-with-the-unique-identifier/)|[Solution](../master/database/_1378.sql) || Easy |
945946
|1369|[Get the Second Most Recent Activity](https://leetcode.com/problems/get-the-second-most-recent-activity/)|[Solution](../master/database/_1369.sql) || Hard |

database/_1407.sql

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
--1407. Top Travellers
2+
--
3+
--Table: Users
4+
--
5+
--+---------------+---------+
6+
--| Column Name | Type |
7+
--+---------------+---------+
8+
--| id | int |
9+
--| name | varchar |
10+
--+---------------+---------+
11+
--id is the primary key for this table.
12+
--name is the name of the user.
13+
--
14+
--
15+
--Table: Rides
16+
--
17+
--+---------------+---------+
18+
--| Column Name | Type |
19+
--+---------------+---------+
20+
--| id | int |
21+
--| user_id | int |
22+
--| distance | int |
23+
--+---------------+---------+
24+
--id is the primary key for this table.
25+
--city_id is the id of the city who bought the product "product_name".
26+
--
27+
--
28+
--Write an SQL query to report the distance travelled by each user.
29+
--
30+
--Return the result table ordered by travelled_distance in descending order, if two or more users travelled the same distance, order them by their name in ascending order.
31+
--
32+
--The query result format is in the following example.
33+
--
34+
--Users table:
35+
--+------+-----------+
36+
--| id | name |
37+
--+------+-----------+
38+
--| 1 | Alice |
39+
--| 2 | Bob |
40+
--| 3 | Alex |
41+
--| 4 | Donald |
42+
--| 7 | Lee |
43+
--| 13 | Jonathan |
44+
--| 19 | Elvis |
45+
--+------+-----------+
46+
--
47+
--Rides table:
48+
--+------+----------+----------+
49+
--| id | user_id | distance |
50+
--+------+----------+----------+
51+
--| 1 | 1 | 120 |
52+
--| 2 | 2 | 317 |
53+
--| 3 | 3 | 222 |
54+
--| 4 | 7 | 100 |
55+
--| 5 | 13 | 312 |
56+
--| 6 | 19 | 50 |
57+
--| 7 | 7 | 120 |
58+
--| 8 | 19 | 400 |
59+
--| 9 | 7 | 230 |
60+
--+------+----------+----------+
61+
--
62+
--Result table:
63+
--+----------+--------------------+
64+
--| name | travelled_distance |
65+
--+----------+--------------------+
66+
--| Elvis | 450 |
67+
--| Lee | 450 |
68+
--| Bob | 317 |
69+
--| Jonathan | 312 |
70+
--| Alex | 222 |
71+
--| Alice | 120 |
72+
--| Donald | 0 |
73+
--+----------+--------------------+
74+
--Elvis and Lee travelled 450 miles, Elvis is the top traveller as his name is alphabetically smaller than Lee.
75+
--Bob, Jonathan, Alex and Alice have only one ride and we just order them by the total distances of the ride.
76+
--Donald didn't have any rides, the distance travelled by him is 0.
77+
--
78+
--# Write your MySQL query statement below
79+
80+
--credit: https://leetcode.com/problems/top-travellers/discuss/572803/MySQL-Simple-Solution
81+
select name, sum(ifnull(distance, 0)) as travelled_distance
82+
from rides r
83+
right join users u
84+
on r.user_id = u.id
85+
group by name
86+
order by 2 desc,1 asc;

0 commit comments

Comments
 (0)