- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres-SELECT.sql
More file actions
Latest commit
executable file
·367 lines (304 loc) · 10.6 KB
/
Copy pathpostgres-SELECT.sql
File metadata and controls
executable file
·367 lines (304 loc) · 10.6 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
-- SQL
-- syntax
-- SELECT A1, A2, ...
-- [FROM T1]
-- [JOIN T2 ON cond]
-- [WHERE cond]
-- [ORDER BY col [{ASC|DESC}], ...col]
-- [LIMIT n]
-- ---------------------------------
-- explicit JOIN vs multiple tables in FROM clause
-- Completely joined
-- and let's try various ways how to sort the result set, ascending, descending, using several fields
-- what are the e, ro, r? Why are they there?
-- ---------------------------------
SELECTe.name, e.experience, ro.name, r.reservation_date
FROM employees e
JOIN reservations r ONe.id=r.employee_id
JOIN rooms ro ONr.room_id=ro.id
ORDER BY experience DESC, e.nameASC;
SELECTe.name, e.experience, ro.name, r.reservation_date
FROM employees e, rooms ro, reservations r
WHEREe.id=r.employee_idANDro.id=r.room_id
-- ---------------------------------
-- We want all the employees having the same xp
-- the problem is that we got the combination with self and duplicities
-- let's try to add a WHERE clause to solve it
-- ---------------------------------
EXPLAIN SELECTe1.id,e1.name, e1.experience, e2.id,e2.name,e2.experience
FROM employees e1
JOIN employees e2 ONe1.experience=e2.experienceANDe1.id<e2.id;
EXPLAIN SELECTe1.id,e1.name, e1.experience, e2.id,e2.name,e2.experience
FROM employees e1
JOIN employees e2 ONe1.experience=e2.experience
WHEREe1.id<e2.id;
-- ---------------------------------
-- We can use SET operations, such as UNION, INTERSECT, ...
-- UNION returns distinct values (like a true set operation), UNION ALL does not
-- we want all names (employees, rooms) in a single table
-- what is the name of the column in the result?
-- ---------------------------------
SELECT name AS label FROM employees
UNION ALL
SELECT name FROM rooms
ORDER BY label;
-- ---------------------------------
-- INTERESECT (not really required, as we have JOIN)
-- we want ids of those employees who reserved room 2 and 3
-- ---------------------------------
SELECT employee_id FROM reservations WHERE room_id =2
INTERSECT
SELECT employee_id FROM reservations WHERE room_id =3
-- ---------------------------------
-- the same thing through JOIN
-- we want employees who reserved rooms 2 and 3
-- ---------------------------------
SELECTr1.employee_id
FROM reservations r1
JOIN reservations r2 ONr1.employee_id=r2.employee_id
WHEREr1.room_id=2ANDr2.room_id=3;
-- ---------------------------------
-- another way, using subselect and `IN`
-- ---------------------------------
SELECTr1.employee_id
FROM reservations r1
WHEREr1.room_id=2AND employee_id IN
(SELECTr2.employee_idFROM reservations r2 wherer2.room_id=3);
-- ---------------------------------
-- MINUS (EXCEPT)
-- we want those who reserved 1 and did not reserved 4
-- ---------------------------------
SELECT employee_id FROM reservations WHERE room_id =1
EXCEPT
SELECT employee_id FROM reservations WHERE room_id =4;
-- ---------------------------------
-- Another way how to achieve the same thing, using NOT IN
-- ---------------------------------
SELECT DISTINCTr1.employee_id
FROM reservations r1
WHEREr1.room_id=1
ANDr1.employee_id NOT IN
(SELECTr1.employee_id
FROM reservations r1
WHEREr1.room_id=4);
-- ---------------------------------
-- All employees, who reserved 1, using JOIN
-- ---------------------------------
SELECTavg(DISTINCT e.experience) FROM employees e
JOIN reservations r ONr.employee_id=e.id
WHEREr.room_id=1;
-- ---------------------------------
-- the same using subselect
-- ---------------------------------
SELECTavg(e.experience) FROM employees e
WHEREe.idIN
(SELECT employee_id FROM reservations WHERE room_id =1);
-- ---------------------------------
-- The first one (using JOIN) might be a problem, because it is returning rows from reservations,
-- therefore, we can have one employee present multiple times within the results
-- in this particular case, we can use `distinct` to solve it, but what if we requrie experience instead of id...?
-- ===============================================================================
-- ---------------------------------
-- NOT EXISTS, ALL, ANY
-- ---------------------------------
-- max xp through subquery (try MIN as well)
-- ---------------------------------
SELECT*FROM employees
WHERE experience = (SELECTmax(experience) FROM employees)
SELECTs1.id,s1.name,s1.experience
FROM employees s1
WHERE NOT EXISTS (SELECT*FROM employees s2 WHEREs1.experience<s2.experience);
-- ---------------------------------
-- The same can be accomplished by two other means
-- why there is an equal sign? Try to put it away
-- ---------------------------------
SELECT*FROM employees s1
wheres1.experience>= ALL (SELECT experience FROM employees s2);
-- ---------------------------------
SELECT*FROM employees s1
where NOT s1.experience< ANY (SELECT experience FROM employees s2);
-- ---------------------------------
-- Toy example. I want to adjust the column and filter by it
SELECT*, experience *2as double_xp FROM employees
WHERE experience *2>5
SELECT*, experience *2as double_xp FROM employees
WHERE experience *2>5
ORDER BY double_xp DESC
-- subselect in FROM clause
SELECT*
FROM (select id, name, experience *2as double_xp
from employees) tmp2
where double_xp >5;
WITH double_xp_table AS (
select id, name, experience *2as double_xp from employees
)
SELECT*FROM double_xp_table
WHERE double_xp >5;
SELECT'1'
-- subselect in attributes (just to show that it works, must be a single value though)
SELECTr.name, r.capacity, '1'as my_column FROM rooms r;
-- all rooms and the highest xp for each of them (show subselect)
SELECTro.name,
(SELECT DISTINCT experience
FROM employees e
JOIN reservations r ONe.id=r.employee_id
WHEREr.room_id=ro.id
AND experience >= ALL (SELECTe.experienceFROM employees e
JOIN reservations r ONe.id=r.employee_id
WHEREr.room_id=ro.id)
) as xp
FROM rooms ro;
SELECTro.name,
(SELECTmax(experience)
FROM employees e
JOIN reservations r ONe.id=r.employee_id
WHEREr.room_id=ro.id
) as xp
FROM rooms ro;
-- if I was to retrieve name instead of xp, I would fail (it returns more rows)
SELECTro.name,
(SELECT DISTINCTe.name
FROM employees e
JOIN reservations r ONe.id=r.employee_id
WHEREr.room_id=ro.id
) as name
FROM rooms ro;
-- Aggregations
-- SELECT A1,...,An, left(name, 12) COUNT, MAX, MIN, AVG, SUM
-- FROM T1
-- [JOIN T2, JOIN T3...]
-- [WHERE cond]
-- [GROUP BY attrs]
-- [HAVING cond2]
-- average of all employees
SELECTavg(experience) as average FROM employees;
-- average of all employess, who reserved 'Toughjoyfax'
SELECTavg(experience)
FROM employees e
JOIN reservations r ONr.employee_id=e.id
JOIN rooms ro ONro.id=r.room_id
WHEREro.nameLIKE'Toughjoyfax';
-- Is it really ok? Watch out for duplicate rows, JOIN can give me an employee several times
-- so once again
SELECTavg(experience)
FROM employees e
wheree.idIN
(SELECTr.employee_id
FROM reservations r
JOIN rooms ro ONro.id=r.room_id
WHEREro.nameLIKE'Toughjoyfax');
-- number of rooms where capacity is greater than
SELECTcount(*)
FROM rooms r
WHERE capacity >90;
SELECT*FROM rooms;
-- number of reservations for Toughjoyfax
SELECTcount(*)
FROM reservations r
JOIN rooms ro ONro.id=r.room_id
WHEREro.nameLIKE'Toughjoyfax';
SELECT*
FROM reservations r
JOIN rooms ro ONro.id=r.room_id
WHEREro.nameLIKE'Toughjoyfax';
-- number of people, who reserved Toughjoyfax
-- notice the distinct
SELECTcount(distinct employee_id)
FROM reservations r
JOIN rooms ro ONro.id=r.room_id
WHEREro.nameLIKE'Toughjoyfax';
-- GROUP BY
SELECT employee_id, count(*) count
FROM reservations
GROUP BY employee_id;
SELECT*
FROM reservations
ORDER BY employee_id;
SELECT*FROM rooms;
-- let's add location to rooms
ALTERTABLE rooms
ADD COLUMN location VARCHAR(50) DEFAULT 'A1'
UPDATE rooms
SET location ='B1'WHERE id IN (4,5)
-- if we want to see&tune what GROUP BY would do, let's simulate it via ORDER BY
SELECT*FROM rooms ORDER BY location;
-- we want total capacity of individual locations
SELECT location, sum(capacity)
FROM rooms
GROUP BY location;
SELECT*FROM reservations
ORDER BY employee_id, room_id
-- who reserved what, how many times
SELECT employee_id, room_id, count(*)
FROM reservations
GROUP BY employee_id, room_id;
-- can I ask for another attribute that the one I am grouping by
-- what would be its value?
SELECTr.employee_id, e.name, count(*)
FROM reservations r
JOIN employees e ONr.employee_id=e.id
GROUP BYr.employee_id, e.name
-- at least if we group by something, which is known as primary key to postgres
SELECTe.id, e.name, count(*)
FROM reservations r
JOIN employees e ONe.id=r.employee_id
GROUP BYe.id
-- a quick check
SELECTr.employee_id, e.name
FROM reservations r
JOIN employees e ONr.employee_id=e.id
ORDER BY employee_id
-- we want also the employees who did not reserve any room ever
-- using union...
SELECTe.name, count(distinct room_id) as pocet
FROM reservations r
JOIN employees e ONr.employee_id=e.id
GROUP BY employee_id, e.name
UNION
SELECT name, 0as kount FROM employees WHERE id NOT IN (SELECT employee_id FROM reservations);
-- better solution, using LEFT JOIN
-- http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/
SELECT*
FROM employees e
LEFT JOIN reservations r ONr.employee_id=e.id
ORDER BYe.id;
SELECTe.name, COUNT(room_id)
FROM employees e
LEFT JOIN reservations r ONe.id=r.employee_id
GROUP BYe.id;
-- we want those locations, where we have more than two rooms
-- HAVING allows us to filter groups, which GROUP BY formed
SELECT location, sum(capacity)
FROM rooms
GROUP BY location
HAVINGcount(*) >2;
SELECT location, sum(capacity)
FROM rooms
GROUP BY location;
SELECT*FROM rooms;
-- toy example to show, that we can use complex conditions in HAVING
SELECT location
FROM rooms
GROUP BY location
HAVINGmax(capacity) < (SELECTavg(capacity) FROM rooms);
-- a bit more of syntax
-- DELETE FROM table [WHERE cond]
-- UPDATE table
-- SET attr1 = val1, attr2 = val2
-- [WHERE cond]
-- let's create a record for those, who did not reserve anything yet
INSERT INTO reservations(employee_id, room_id)
SELECTe.idas employee_id, ro.idas room_id
FROM employees e, rooms ro
WHEREe.id NOT IN (SELECT employee_id FROM reservations)
ANDro.nameLIKE'Toughjoyfax';
SELECT*FROM reservations;
-- delete reservations to employees 6 and 7
DELETEFROM reservations where employee_id IN (6,7);
SELECT DISTINCTprog.name
FROM programmers prog
JOIN projects_programmers proj_progs ONproj_progs.programmer_id=prog.id
JOIN projects proj ONproj.id=proj_progs.project_id
JOIN languages l ONproj.language_id=l.id
WHEREl.label='python'ANDprog.idIN (SELECT programmer_id FROM
projects_programmers proj_progs WHERE owner IS TRUE);