-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.sql
423 lines (370 loc) · 6.65 KB
/
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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
SELECT
*
FROM
animals
WHERE
name LIKE '%mon';
SELECT
name
FROM
animals
WHERE
date_of_birth BETWEEN '2016-01-01'
AND '2019-12-31';
SELECT
name
FROM
animals
WHERE
neutered = TRUE
AND escape_attempts < 3;
SELECT
date_of_birth
FROM
animals
WHERE
name IN ('Agumon', 'Pikachu');
SELECT
name,
escape_attempts
FROM
animals
WHERE
weight_kg > 10.5;
SELECT
*
FROM
animals
WHERE
neutered = TRUE;
SELECT
*
FROM
animals
WHERE
name <> 'Gabumon';
SELECT
*
FROM
animals
WHERE
weight_kg BETWEEN 10.4
AND 17.3;
BEGIN;
-- Start the transaction
UPDATE
animals
SET
species = 'unspecified';
-- Verify the change
SELECT
*
FROM
animals;
ROLLBACK;
-- Roll back the transaction
BEGIN;
-- Start the transaction
-- Update species for animals with name ending in "mon"
UPDATE
animals
SET
species = 'digimon'
WHERE
name LIKE '%mon';
-- Update species for animals without a species set
UPDATE
animals
SET
species = 'pokemon'
WHERE
species IS NULL;
-- Verify the changes
SELECT
*
FROM
animals;
COMMIT;
-- Commit the transaction
-- Verify the changes persist after commit
SELECT
*
FROM
animals;
BEGIN;
-- Start the transaction
DELETE FROM
animals;
-- Verify that records were deleted
SELECT
*
FROM
animals;
ROLLBACK;
-- Roll back the transaction
BEGIN;
-- Start the transaction
-- Delete animals born after Jan 1st, 2022
DELETE FROM
animals
WHERE
date_of_birth > '2022-01-01';
-- Create a savepoint
SAVEPOINT weight_update;
-- Update all animals' weights to be their weight multiplied by -1
UPDATE
animals
SET
weight_kg = weight_kg * -1;
-- Rollback to the savepoint
ROLLBACK TO weight_update;
-- Update negative weights to their positive values
UPDATE
animals
SET
weight_kg = ABS(weight_kg)
WHERE
weight_kg < 0;
-- Commit transaction
COMMIT;
SELECT
COUNT(*)
FROM
animals;
SELECT
COUNT(*)
FROM
animals
WHERE
escape_attempts = 0;
SELECT
AVG(weight_kg)
FROM
animals;
SELECT
neutered,
SUM(escape_attempts) AS total_escape_attempts
FROM
animals
GROUP BY
neutered;
SELECT
species,
MIN(weight_kg) AS min_weight,
MAX(weight_kg) AS max_weight
FROM
animals
GROUP BY
species;
SELECT
species,
AVG(escape_attempts)
FROM
animals
WHERE
date_of_birth >= '1990/01/01'
AND date_of_birth <= '2000/12/31'
GROUP BY
species;
SELECT
o.full_name AS owner_name,
a.name AS animal_name
FROM
owners o
JOIN animals a ON o.id = a.owner_id
WHERE
o.full_name = 'Melody Pond';
SELECT
a.name
FROM
animals a
JOIN species s ON a.species_id = s.id
WHERE
s.name = 'Pokemon';
SELECT
o.full_name,
COALESCE(array_agg(a.name), ARRAY [] :: VARCHAR [])
FROM
owners o
LEFT JOIN animals a ON o.id = a.owner_id
GROUP BY
o.full_name;
SELECT
s.name,
COUNT(a.id) AS animal_count
FROM
species s
LEFT JOIN animals a ON s.id = a.species_id
GROUP BY
s.name;
SELECT
a.name
FROM
animals a
JOIN owners o ON a.owner_id = o.id
JOIN species s ON a.species_id = s.id
WHERE
o.full_name = 'Jennifer Orwell'
AND s.name = 'Digimon';
SELECT
owners.full_name AS owner_name,
animals.escape_attempts,
animals.name AS animal_name
FROM
animals
JOIN owners ON animals.owner_id = owners.id
WHERE
owners.full_name = 'Dean Winchester'
AND animals.escape_attempts = 0
UNION
SELECT
'Dean Winchester' AS owner_name,
0 AS escape_attempts,
NULL AS animal_name
LIMIT
1;
SELECT
o.full_name,
COUNT(a.id) AS animal_count
FROM
owners o
LEFT JOIN animals a ON o.id = a.owner_id
GROUP BY
o.full_name
ORDER BY
animal_count DESC
LIMIT
1;
-- Who was the last animal seen by William Tatcher?
SELECT
a.name AS animal_name
FROM
animals a
JOIN visits v ON a.id = v.animal_id
JOIN vets vt ON v.vet_id = vt.id
WHERE
vt.name = 'William Tatcher'
ORDER BY
v.visit_date DESC
LIMIT
1;
-- How many different animals did Stephanie Mendez see?
SELECT
COUNT(DISTINCT v.animal_id) AS num_animals_seen
FROM
visits v
JOIN vets vt ON v.vet_id = vt.id
WHERE
vt.name = 'Stephanie Mendez';
-- List all vets and their specialties, including vets with no specialties.
SELECT
v.name AS vet_name,
s.name AS specialty
FROM
vets v
LEFT JOIN specializations sp ON v.id = sp.vet_id
LEFT JOIN species s ON sp.species_id = s.id;
-- List all animals that visited Stephanie Mendez between April 1st and August 30th, 2020.
SELECT
a.name AS animal_name,
v.visit_date
FROM
animals a
JOIN visits v ON a.id = v.animal_id
JOIN vets vt ON v.vet_id = vt.id
WHERE
vt.name = 'Stephanie Mendez'
AND v.visit_date BETWEEN '2020-04-01'
AND '2020-08-30';
-- What animal has the most visits to vets?
SELECT
a.name AS animal_name,
COUNT(v.animal_id) AS num_visits
FROM
animals a
JOIN visits v ON a.id = v.animal_id
GROUP BY
a.name
ORDER BY
num_visits DESC
LIMIT
1;
-- Who was Maisy Smith's first visit?
SELECT
a.name AS animal_name,
v.visit_date
FROM
animals a
JOIN visits v ON a.id = v.animal_id
JOIN vets vt ON v.vet_id = vt.id
JOIN owners o ON a.owner_id = o.id
WHERE
o.full_name = 'Maisy Smith'
ORDER BY
v.visit_date ASC
LIMIT
1;
-- Details for most recent visit: animal information, vet information, and date of visit.
SELECT
a.name AS animal_name,
v.visit_date,
vt.name AS vet_name
FROM
animals a
JOIN visits v ON a.id = v.animal_id
JOIN vets vt ON v.vet_id = vt.id
ORDER BY
v.visit_date DESC
LIMIT
1;
-- How many visits were with a vet that did not specialize in that animal's species?
SELECT
COUNT(*) AS num_visits
FROM
visits v
JOIN animals a ON v.animal_id = a.id
JOIN vets vt ON v.vet_id = vt.id
LEFT JOIN specializations sp ON vt.id = sp.vet_id
AND a.species_id = sp.species_id
WHERE
sp.vet_id IS NULL;
-- What specialty should Maisy Smith consider getting? Look for the species she gets the most.
SELECT
s.name AS potential_specialty,
COUNT(*) AS num_visits
FROM
visits v
JOIN animals a ON v.animal_id = a.id
JOIN owners o ON a.owner_id = o.id
JOIN specializations sp ON a.species_id = sp.species_id
JOIN species s ON sp.species_id = s.id
WHERE
o.full_name = 'Maisy Smith'
GROUP BY
s.name
ORDER BY
num_visits DESC
LIMIT
1;
-- EXPLAIN ANALYZE with Query
EXPLAIN ANALYZE
SELECT
COUNT(*)
FROM
visit
where
animal_id = 4;
EXPLAIN ANALYZE
SELECT
*
FROM
visit
where
vet_id = 2;
EXPLAIN ANALYZE
SELECT
*
FROM
owners
where
email = 'owner_18327@mail.com';