|
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 | 1 | --# Write your MySQL query statement below
|
79 | 2 |
|
80 | 3 | --credit: https://leetcode.com/problems/top-travellers/discuss/572803/MySQL-Simple-Solution
|
|
0 commit comments