-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtop_travellers.sql
More file actions
54 lines (46 loc) · 988 Bytes
/
top_travellers.sql
File metadata and controls
54 lines (46 loc) · 988 Bytes
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
-- https://leetcode.com/problems/top-travellers/
CREATE TABLE Users (
id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
-- Insert into Users
INSERT INTO Users (id, name) VALUES
(1, 'Alice'),
(2, 'Bob'),
(3, 'Alex'),
(4, 'Donald'),
(7, 'Lee'),
(13, 'Jonathan'),
(19, 'Elvis');
-- Create Rides table
CREATE TABLE Rides (
id INT PRIMARY KEY,
user_id INT,
distance INT,
FOREIGN KEY (user_id) REFERENCES Users(id)
);
-- Insert into Rides
INSERT INTO Rides (id, user_id, distance) VALUES
(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);
SELECT name, COALESCE(SUM(distance),0) AS travelled_distance
FROM Users
LEFT JOIN Rides ON
Users.id = Rides.user_id
GROUP BY user_id
ORDER BY travelled_distance DESC,
name ASC
SELECT name, ifnull(SUM(distance),0) AS travelled_distance
FROM Users
LEFT JOIN Rides ON
Users.id = Rides.user_id
GROUP BY user_id
ORDER BY travelled_distance DESC,
name ASC