-
Notifications
You must be signed in to change notification settings - Fork 525
/
Copy path30_Simple_ SQL_Queries.sql
271 lines (234 loc) · 5.88 KB
/
30_Simple_ SQL_Queries.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/****************************************/
/* 30 Simple SQL Interview Queries */
/****************************************/
/*1. Delete table Employee, Department and Company.*/
DROP TABLE IF EXISTS Employee;
DROP TABLE IF EXISTS Department;
DROP TABLE IF EXISTS Company;
/*
2. Create tables:
Employee with attributes (id, name, city, department, salary)
Department with attributes (id, name)
Company with attributes (id, name, revenue)
*/
CREATE TABLE department(
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL
);
CREATE TABLE company(
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
revenue INT
);
CREATE TABLE Employee(
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(150) NOT NULL,
city VARCHAR(150) NOT NULL,
department_id INT NOT NULL,
salary INT NOT NULL,
FOREIGN KEY (department_id) REFERENCES department(id)
);
/*
4. Add rows into Department table
(1, 'IT'),
(2, 'Management'),
(3, 'IT'),
(4, 'Support');
*/
INSERT INTO department(name)
VALUES
('IT'),
('Management'),
('IT'),
('Support');
/*
5. Add rows into Company table
(1, 'IBM', 2000000),
(2, 'GOOGLE', 9000000),
(3, 'Apple', 10000000);
*/
INSERT INTO company(name,revenue)
VALUES
('IBM', 2000000),
('GOOGLE', 9000000),
('Apple', 10000000);
/*
3.Add rows into employee table:
(1, 'David', 'London', 'IT', 80000),
(2, 'Emily', 'London', 'IT', 70000),
(3, 'Peter', 'Paris', 'IT', 60000),
(4, 'Ava', 'Paris', 'IT', 50000),
(5, 'Penny', 'London', 'Management', 110000),
(6, 'Jim', 'London', 'Management', 90000),
(7, 'Amy', 'Rome', 'Support', 30000),
(8, 'Cloe', 'London', 'IT', 110000);
*/
INSERT INTO employee (name,city,department_id,salary)
VALUES
('David', 'London', 3, 80000),
('Emily', 'London', 3, 70000),
('Peter', 'Paris', 3, 60000),
('Ava', 'Paris', 3, 50000),
('Penny', 'London', 2, 110000),
('Jim', 'London', 2, 90000),
('Amy', 'Rome', 4, 30000),
('Cloe', 'London', 3, 110000);
/*
6. Query all rows from Department table
*/
SELECT * FROM department;
/*
7. Change the name of department with id = 1 to 'Management'
*/
UPDATE department
SET name = 'Management'
WHERE id = 1;
/*
8. Delete employees with salary greater than 100 000
*/
DELETE FROM employee
WHERE salary > 100000;
/*
9. Query the names of companies
*/
SELECT name FROM company;
/*
10. Query the name and city of every employee
*/
SELECT name, city
FROM employee;
/*
11. Query all companies with revenue greater than 5 000 000
*/
SELECT * FROM company
WHERE revenue > 5000000;
/*
12. Query all companies with revenue smaller than 5 000 000
*/
SELECT * FROM company
WHERE revenue < 5000000;
/*
13. Query all companies with revenue smaller than 5 000 000, but you cannot use the '<' operator
*/
SELECT * FROM company
ORDER BY revenue
LIMIT 1;
/*version 2*/
SELECT * FROM company
WHERE NOT revenue >= 5000000;
/*
14. Query all employees with salary greater than 50 000 and smaller than 70 000
*/
SELECT * FROM employee
WHERE salary BETWEEN 50000 AND 70000;
/*
15. Query all employees with salary greater than 50 000 and smaller than 70 000, but you cannot use BETWEEN
*/
SELECT * FROM employee
WHERE salary >= 50000 AND salary <= 70000;
/*
16. Query all employees with salary equal to 80 000
*/
SELECT * FROM employee
WHERE salary = 80000;
/*
17. Query all employees with salary not equal to 80 000
*/
SELECT * FROM employee
WHERE salary <> 80000;
/*
18. Query all names of employees with salary greater than 70 000 together with employees who work on the 'IT' department.
*/
SELECT name FROM employee
WHERE salary > 70000
OR department_id IN (
SELECT id FROM department
WHERE name = 'IT'
);
/*
19. Query all employees that work in city that starts with 'L'
*/
SELECT * FROM employee
WHERE city LIKE 'L%';
/*
20. Query all employees that work in city that starts with 'L' or ends with 's'
*/
SELECT * FROM employee
WHERE city LIKE 'L%' OR city LIKE '%s';
/*
21. Query all employees that work in city with 'o' somewhere in the middle
*/
SELECT * FROM employee
WHERE city LIKE '%o%';
/*
22. Query all departments (each name only once)
*/
SELECT DISTINCT name FROM department;
/*
22. Query names of all employees together with id of department they work in, but you cannot use JOIN
*/
SELECT emp.name,dep.id,dep.name
FROM employee emp, department dep
WHERE emp.department_id = dep.id
ORDER BY emp.name, dep.id;
/*
23. Query names of all employees together with id of department they work in, using JOIN
*/
SELECT emp.name,dep.id,dep.name
FROM employee emp
JOIN department dep
ON emp.department_id = dep.id
ORDER BY emp.name, dep.id;
/*
24. Query name of every company together with every department
Personal thoughts: It is kinda weird question, as there is no relationship between company and departement
*/
SELECT com.name,dep.name
FROM company com, department dep
ORDER BY com.name;
/*
25. Query name of every company together with departments without the 'Support' department
*/
SELECT com.name,dep.name
FROM company com, department dep
WHERE dep.name NOT LIKE 'Support'
ORDER BY com.name;
/*
26. Query employee name together with the department name that they are not working in
*/
SELECT emp.name, dep.name
FROM employee emp, department dep
WHERE emp.department_id <> dep.id;
/*
27. Query company name together with other companies names
LIKE:
GOOGLE Apple
GOOGLE IBM
Apple IBM
...
*/
SELECT com1.name, com2.name
FROM company com1, company com2
WHERE com1.name <> com2.name
ORDER BY com1.name,com2.name;
/*
28. Query employee names with salary smaller than 80 000 without using NOT and <
NOTE: for POSTGRESQL only. Mysql doesn't support except
*/
SELECT e1.name FROM employee e1
EXCEPT
SELECT e2.name FROM employee e2 WHERE e2.salary >= 80000;
/*
29.Query names of every company and change the name of column to 'Company'
*/
SELECT name AS Company
FROM company;
/*
30. Query all employees that work in same department as Peter
*/
SELECT * FROM employee
WHERE department_id IN(
SELECT department_id FROM employee
WHERE name LIKE 'Peter'
)
AND name NOT LIKE 'Peter';