-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode-Select.sql
More file actions
124 lines (103 loc) · 2.71 KB
/
LeetCode-Select.sql
File metadata and controls
124 lines (103 loc) · 2.71 KB
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
/*
1757 Recyclable and Low Fat Products
Find ids of products that are both low fat and recyclable.
*/
CREATE TABLE products (
product_id INT PRIMARY KEY,
low_fats ENUM ('Y', 'N'),
recyclable ENUM ('Y', 'N')
);
-- Add autoincrement to product_id to make input of values easier
ALTER TABLE products
MODIFY product_id INT AUTO_INCREMENT;
-- Insert values as in the example - more on https://leetcode.com/
INSERT INTO products VALUES(0, 'Y', 'N');
INSERT INTO products(low_fats, recyclable) VALUES('N', 'N');
-- Solution
SELECT product_id FROM Products
WHERE low_fats = 'Y' AND recyclable = 'Y';
-----------------------------------------
/*
584. Find Customer Referee
Find names of customer that are not referred by customer with id = 2.
*/
CREATE TABLE Customer (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(20),
referee_id INT
);
-- Insert values as in the example - more on https://leetcode.com/
INSERT INTO Customer(name, referee_id) VALUES('Mark', 2);
-- Solution
SELECT name
FROM Customer
WHERE referee_id IS NULL OR referee_id <> 2;
-----------------------------------------
/*
595. Big Countries
A country is big if:
* it has an area >= 3000000 km2
or
* it has a population >= 25000000.
Write a solution to find name, population, area of big countries.
*/
CREATE TABLE World (
name VARCHAR(20),
continent VARCHAR(20),
area INT,
population INT,
gdp BIGINT,
PRIMARY KEY (name)
);
-- Insert values as in the example - more on https://leetcode.com/
INSERT INTO World VALUES('Angola', 'Africa', 1246700, 20609294, 100990000000);
-- Solution
SELECT name, population, area
FROM World
WHERE area >= 3000000 OR population >= 25000000;
/*
1148 Article Views I
Find all authors that viewed >= 1 of their own articles.
Return result table sorted by id in ascending order.
*/
CREATE TABLE Views (
article_id INT,
author_id INT,
viewer_id INT,
view_date DATE
);
-- Insert values as in the example
INSERT INTO Views VALUES
(1, 3, 5, '2019-08-01'),
(1, 3, 6, '2019-08-02'),
(2, 7, 7, '2019-08-01'),
(2, 7, 6, '2019-08-02'),
(4, 7, 1, '2019-07-22'),
(3, 4, 4, '2019-07-21'),
(3, 4, 4, '2019-07-21');
-- Solution
SELECT DISTINCT author_id as id
FROM Views
WHERE author_id = viewer_id
ORDER BY id;
-----------------------------------------
/*
1683 Invalid Tweets
Find IDs of invalid tweets (num of chars in content > 15)
*/
CREATE TABLE Tweets (
tweet_id INT,
content VARCHAR(20),
PRIMARY KEY (tweet_id)
);
-- Extended content to 40 chars to fit Tweet 2
ALTER TABLE Tweets
MODIFY content VARCHAR(40);
-- Insert values as in the example
INSERT INTO Tweets VALUES
(1, 'Vote for Biden'),
(2, 'Let us make America great again!');
-- Solution
SELECT tweet_id
FROM Tweets
WHERE LENGTH(content) > 15;