forked from leetlab11/Advanced-SQL-50
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08-01407-top-travellers.sql
126 lines (120 loc) · 3.98 KB
/
08-01407-top-travellers.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
## 1407. Top Travellers
-- 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.
--
-- The result format is in the following example.
--
--
--
-- Example 1:
--
-- Input:
-- Users table:
-- +------+-----------+
-- | id | name |
-- +------+-----------+
-- | 1 | Alice |
-- | 2 | Bob |
-- | 3 | Alex |
-- | 4 | Donald |
-- | 7 | Lee |
-- | 13 | Jonathan |
-- | 19 | Elvis |
-- +------+-----------+
-- Rides table:
-- +------+----------+----------+
-- | id | user_id | distance |
-- +------+----------+----------+
-- | 1 | 1 | 120 |
-- | 2 | 2 | 317 |
-- | 3 | 3 | 222 |
-- | 4 | 7 | 100 |
-- | 5 | 13 | 312 |
-- | 6 | 19 | 50 |
-- | 7 | 7 | 120 |
-- | 8 | 19 | 400 |
-- | 9 | 7 | 230 |
-- +------+----------+----------+
-- Output:
-- +----------+--------------------+
-- | name | travelled_distance |
-- +----------+--------------------+
-- | Elvis | 450 |
-- | Lee | 450 |
-- | Bob | 317 |
-- | Jonathan | 312 |
-- | Alex | 222 |
-- | Alice | 120 |
-- | Donald | 0 |
-- +----------+--------------------+
-- Explanation:
-- Elvis and Lee traveled 450 miles, Elvis is the top traveler as his name is alphabetically smaller than Lee.
-- Bob, Jonathan, Alex, and Alice have only one ride and we just order them by the total distances of the ride.
-- Donald did not have any rides, the distance traveled by him is 0.
--
-- SCHEMA:
Create Table If Not Exists Users (id int, name varchar(30))
Create Table If Not Exists Rides (id int, user_id int, distance int)
Truncate table Users
insert into Users (id, name) values ('1', 'Alice')
insert into Users (id, name) values ('2', 'Bob')
insert into Users (id, name) values ('3', 'Alex')
insert into Users (id, name) values ('4', 'Donald')
insert into Users (id, name) values ('7', 'Lee')
insert into Users (id, name) values ('13', 'Jonathan')
insert into Users (id, name) values ('19', 'Elvis')
Truncate table Rides
insert into Rides (id, user_id, distance) values ('1', '1', '120')
insert into Rides (id, user_id, distance) values ('2', '2', '317')
insert into Rides (id, user_id, distance) values ('3', '3', '222')
insert into Rides (id, user_id, distance) values ('4', '7', '100')
insert into Rides (id, user_id, distance) values ('5', '13', '312')
insert into Rides (id, user_id, distance) values ('6', '19', '50')
insert into Rides (id, user_id, distance) values ('7', '7', '120')
insert into Rides (id, user_id, distance) values ('8', '19', '400')
insert into Rides (id, user_id, distance) values ('9', '7', '230')
-- Solution
-- Oracle
select U.name,
NVL(sum(R.distance),0) travelled_distance
from Users U
left join Rides R on U.id=R.User_id
group by U.name, U.id
order by 2 desc NULLS Last, 1 ASC;
-- MySQL
-- using ifnull around sum()- can also use coalesce
select u.name, ifnull(sum(r.distance), 0) as travelled_distance
from Users u
left join Rides r
on u.id = r.user_id
group by u.id
order by 2 desc, 1 asc
-- point72- 1