-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_crud.sql
270 lines (206 loc) · 4.58 KB
/
02_crud.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
SELECT *
FROM employees;
SELECT id, first_name, last_name, job_title, department_id, salary
FROM employees;
SELECT id, first_name, last_name, job_title
FROM employees
ORDER BY id;
SELECT
id AS '#',
first_name AS 'First Name',
last_name AS 'Last Name',
job_title AS 'Title'
FROM employees;
SELECT *
FROM hotel.employees;
SELECT *
FROM intro_test.employees;
SELECT
employees.id,
employees.first_name,
employees.last_name
FROM hotel.employees;
SELECT
e.id,
e.first_name,
e.last_name
FROM hotel.employees AS e;
SELECT
id,
CONCAT(first_name, ' ', last_name, ' - ', salary, 'lv.') AS 'Full Info'
FROM employees;
SELECT
id,
CONCAT_WS(' >> ', first_name, last_name, job_title, salary) AS 'Separated Info'
FROM employees;
SELECT
salary,
last_name,
first_name,
id
FROM employees;
SELECT
id,
CONCAT(first_name, ' ', last_name) as 'full_name',
job_title,
salary
FROM employees
WHERE salary > 1000
ORDER BY id;
SELECT
id, first_name
FROM employees
WHERE 2 > 10;
SELECT
id, first_name
FROM employees
WHERE department_id = 1;
SELECT
id,
job_title,
salary
FROM employees
WHERE salary > 1000
ORDER BY id;
SELECT DISTINCT
id, first_name
FROM employees;
SELECT DISTINCT
first_name
FROM employees;
-- After changing John salary from 880 -> 900
SELECT DISTINCT
first_name, salary
FROM employees;
SELECT *
FROM employees
WHERE salary > 1000 AND department_id = 4;
SELECT *
FROM employees
WHERE department_id = 3 OR department_id = 4;
SELECT *
FROM employees
WHERE salary > 1100 AND salary < 2000;
SELECT *
FROM employees
WHERE salary BETWEEN 1100 AND 2000;
SELECT *
FROM employees
WHERE department_id = 4 AND salary >= 1000
ORDER BY id;
SELECT *
FROM employees
WHERE
department_id = 3 OR
department_id = 4 OR
department_id = 5 OR
department_id = 6;
SELECT *
FROM employees
WHERE
department_id IN (3, 4, 5, 6);
SELECT *
FROM employees
WHERE first_name = 'jOHN';
-- <> !=
SELECT *
FROM employees
WHERE NOT first_name = 'jOHN';
SELECT *
FROM employees
WHERE NOT (first_name = 'john' OR department_id = 3);
SELECT *
FROM employees
ORDER BY salary DESC, department_id DESC;
SELECT
id AS 'No.',
CONCAT(first_name, ' ', last_name) AS 'Full Name',
salary as 'Salary'
FROM employees
WHERE department_id = 1 OR department_id = 2
ORDER BY first_name, last_name;
CREATE VIEW `v_dept_1_2_info` AS
SELECT
id AS 'No.',
CONCAT(first_name, ' ', last_name) AS 'Full Name',
salary as 'Salary'
FROM employees
WHERE department_id = 1 OR department_id = 2
ORDER BY first_name, last_name;
SELECT * FROM v_dept_1_2_info;
SELECT *
FROM employees
ORDER BY salary DESC
LIMIT 1;
CREATE VIEW `v_top_paid_employee` AS
SELECT *
FROM employees
ORDER BY salary DESC
LIMIT 1;
SELECT * FROM v_top_paid_employee;
-- Error no info for job_title
INSERT INTO `employees`(`first_name`, `last_name`, `salary`)
VALUES ('New', 'Entry', 2000);
INSERT INTO `employees`(
`first_name`, `last_name`, `salary`, `job_title`, `department_id`
)
VALUES ('New', 'Entry', 2000, NULL, NULL);
INSERT INTO departments
VALUES (5, 'New dept');
SELECT * FROM departments;
INSERT INTO departments(id) VALUES (6);
INSERT INTO departments(name)
VALUES
('Sales'),
('R&D'),
('Cloud');
SELECT * FROM departments;
INSERT INTO departments(name) VALUES (NULL);
SELECT first_name, last_name, 1
FROM employees
WHERE department_id = 4;
INSERT INTO clients(first_name, last_name, room_id)
SELECT first_name, last_name, 1
FROM employees
WHERE department_id = 4;
SELECT * FROM clients;
CREATE TABLE `selected_filtered_employees` AS
SELECT id, first_name, last_name
FROM employees
WHERE department_id = 3;
SELECT * FROM selected_employees;
SELECT * FROM selected_filtered_employees;
SELECT first_name, last_name FROM intro_test.employees;
CREATE TABLE hotel.`intro_employees_copy` AS
SELECT first_name, last_name FROM intro_test.employees;
SELECT * FROM intro_employees_copy;
SELECT id, salary * 1.10
FROM employees
WHERE department_id = 3;
UPDATE employees
SET salary = salary * 1.1
WHERE department_id = 3;
SELECT * FROM employees;
SELECT *
FROM employees
WHERE job_title = 'Manager';
UPDATE employees
SET salary = salary + 100
WHERE job_title = 'manager';
SELECT salary FROM employees;
SELECT * FROM departments;
SELECT * FROM departments
WHERE id IN (5, 6, 7, 8, 9, 10);
DELETE FROM departments
WHERE id IN (5, 6, 7, 8, 9, 10);
DELETE FROM departments
WHERE id = 3;
-- department_id = 1 OR department_id = 2
SELECT *
FROM employees
WHERE department_id IN (1, 2);
DELETE FROM employees
WHERE department_id IN (1, 2);
SELECT *
FROM employees
ORDER BY id;