generated from microverseinc/curriculum-template-databases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.sql
412 lines (365 loc) · 7.41 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
/*Queries that provide answers to the questions from all projects.*/
/* Find all animals whose name ends in "mon" */
SELECT
*
FROM
animals
WHERE
name LIKE '%mon';
/* List the name of all animals born between 2016 and 2019 */
SELECT
name
FROM
animals
WHERE
date_of_birth >= '20160101'
AND date_of_birth < '20200101'
/* List the name of all animals that are neutered and have less than 3 escape attempts */
SELECT
name
FROM
animals
WHERE
neutered = TRUE
AND escape_attempts < 3;
/* List date of birth of all animals named either "Agumon" or "Pikachu" */
SELECT
date_of_birth
FROM
animals
WHERE
name = 'Agumon'
OR name = 'Pikachu';
/* List name and escape attempts of animals that weigh more than 10.5kg */
SELECT
name,
escape_attempts
FROM
animals
WHERE
weight_kg > 10.5;
/* Find all animals that are neutered */
SELECT
*
FROM
animals
WHERE
neutered = TRUE;
/* Find all animals not named Gabumon */
SELECT
*
FROM
animals
WHERE
name <> 'Gabumon';
/* Find all animals with a weight between 10.4kg and 17.3kg (including the animals with the weights that equals precisely 10.4kg or 17.3kg) */
SELECT
*
FROM
animals
WHERE
weight_kg >= 10.4
AND weight_kg <= 17.3;
/*
Inside a transaction update the animals table by setting the species column to unspecified.
Verify that change was made. Then roll back the change and verify that species columns went
back to the state before transaction.
*/
BEGIN;
UPDATE
animals
SET
species = 'unspecified';
SELECT
*
FROM
animals;
ROLLBACK;
SELECT
*
FROM
animals;
/* Inside a transaction... */
BEGIN;
/* Update the animals table by setting the species column to digimon for all animals that have a name ending in mon */
UPDATE
animals
SET
species = 'digimon'
WHERE
name LIKE '%mon';
/* Update the animals table by setting the species column to pokemon for all animals that don't have species already set */
UPDATE
animals
SET
species = 'pokemon'
WHERE
species = '';
/* Commit the transaction */
COMMIT;
/* Delete everthing and then rollback */
BEGIN;
DELETE FROM
animals;
ROLLBACK;
BEGIN;
DELETE FROM
animals
WHERE
date_of_birth > '20220101';
SAVEPOINT delete2022;
UPDATE
animals
SET
weight_kg = weight_kg * (-1);
ROLLBACK TO delete2022;
UPDATE
animals
SET
weight_kg = weight_kg * (-1)
WHERE
weight_kg < 0;
COMMIT;
/* How many animals are there? */
SELECT
COUNT(*)
FROM
animals;
/* How many animals have never tried to escape? */
SELECT
COUNT(*)
FROM
animals
WHERE
escape_attempts = 0;
/* What is the average weight of animals? */
SELECT
AVG(weight_kg)
FROM
animals;
/* Who escapes the most, neutered or not neutered animals? */
SELECT
CASE
sub.neutered
WHEN sub.neutered = TRUE THEN 'Neutered escaped the most'
ELSE 'Not Neutered escaped the most'
END
FROM
(
SELECT
neutered,
SUM(escape_attempts) AS res
FROM
animals
GROUP BY
neutered
) AS sub
ORDER BY
res DESC
LIMIT
1;
/* What is the minimum and maximum weight of each type of animal? */
SELECT
species,
MIN(weight_kg),
MAX(weight_kg)
FROM
animals
GROUP BY
species;
/* What is the average number of escape attempts per animal type of those born between 1990 and 2000? */
SELECT
species,
AVG(escape_attempts)
FROM
animals
WHERE
date_of_birth >= '19900101'
AND date_of_birth < '20010101'
GROUP BY
species;
/* What animals belong to Melody Pond? */
SELECT
a.*
FROM
animals AS a
INNER JOIN owners AS o ON a.owner_id = o.id
WHERE
o.full_name = 'Melody Pond';
/* List of all animals that are pokemon (their type is Pokemon). */
SELECT
a.*
FROM
animals AS a
INNER JOIN species AS s ON a.species_id = s.id
WHERE
s.name = 'Pokemon';
/* List all owners and their animals, remember to include those that don't own any animal. */
SELECT
*
FROM
owners as o
LEFT JOIN animals AS a ON o.id = a.owner_id;
/* How many animals are there per species? */
SELECT
s.name,
COUNT(*)
FROM
animals AS a
INNER JOIN species AS s ON a.species_id = s.id
GROUP BY
s.name;
/* List all Digimon owned by Jennifer Orwell. */
SELECT
a.*
FROM
animals AS a
INNER JOIN owners AS o ON a.owner_id = o.id
INNER JOIN species AS s ON a.species_id = s.id
WHERE
s.name = 'Digimon'
AND o.full_name = 'Jennifer Orwell';
/* List all animals owned by Dean Winchester that haven't tried to escape. */
SELECT
a.*
FROM
animals AS a
INNER JOIN owners AS o ON a.owner_id = o.id
WHERE
o.full_name = 'Dean Winchester'
AND a.escape_attempts = 0;
/* Who owns the most animals? */
SELECT
o.full_name AS fName,
COUNT(a.*) AS num
FROM
owners AS o
INNER JOIN animals AS a ON o.id = a.owner_id
GROUP BY
o.id
ORDER BY
num DESC
LIMIT
1;
/* Who was the last animal seen by William Tatcher? */
SELECT
a.name,
vi.visit_date
FROM
animals AS a
INNER JOIN visits AS vi ON a.id = vi.animals_id
INNER JOIN vets AS ve ON vi.vets_id = ve.id
WHERE
ve.name = 'William Tatcher'
ORDER BY
vi.visit_date DESC
LIMIT
1;
/* How many different animals did Stephanie Mendez see? */
SELECT
DISTINCT COUNT(a.name)
FROM
animals AS a
INNER JOIN visits AS vi ON a.id = vi.animals_id
INNER JOIN vets AS ve ON vi.vets_id = ve.id
WHERE
ve.name = 'Stephanie Mendez';
/* List all vets and their specialties, including vets with no specialties. */
SELECT
ve.name,
se.name
FROM
vets AS ve
LEFT JOIN specializations AS sa ON ve.id = sa.vets_id
LEFT JOIN species AS se ON sa.species_id = se.id;
/* List all animals that visited Stephanie Mendez between April 1st and August 30th, 2020. */
SELECT
a.name
FROM
animals AS a
INNER JOIN visits AS vi ON a.id = vi.animals_id
INNER JOIN vets AS ve ON vi.vets_id = ve.id
WHERE
ve.name = 'Stephanie Mendez'
AND vi.visit_date >= '2020-04-1'
AND vi.visit_date <= '2020-08-30';
/* What animal has the most visits to vets? */
SELECT
a.name,
COUNT(a.name)
FROM
animals AS a
INNER JOIN visits AS vi ON a.id = vi.animals_id
GROUP BY
a.name
ORDER BY
COUNT DESC
LIMIT
1;
/* Who was Maisy Smith's first visit? */
SELECT
a.name,
vi.visit_date
FROM
animals AS a
INNER JOIN visits AS vi ON a.id = vi.animals_id
INNER JOIN vets AS ve ON vi.vets_id = ve.id
WHERE
ve.name = 'Maisy Smith'
ORDER BY
vi.visit_date ASC
LIMIT
1;
/* Details for most recent visit: animal information, vet information, and date of visit. */
SELECT
a.name,
a.date_of_birth,
a.escape_attempts,
a.neutered,
a.weight_kg,
ve.name,
ve.age,
ve.date_of_graduation,
vi.visit_date
FROM
animals AS a
INNER JOIN visits AS vi ON a.id = vi.animals_id
INNER JOIN vets AS ve ON vi.vets_id = ve.id
ORDER BY
vi.visit_date DESC
LIMIT
1;
/* How many visits were with a vet that did not specialize in that animal's species? */
SELECT
COUNT(vi.visit_date)
FROM
vets AS ve
INNER JOIN visits AS vi ON vi.vets_id = ve.id
INNER JOIN animals AS a ON a.id = vi.animals_id
INNER JOIN specializations AS sa ON ve.id = sa.vets_id
INNER JOIN species AS se ON sa.species_id = se.id
WHERE
a.species_id <> sa.species_id;
/* What specialty should Maisy Smith consider getting? Look for the species she gets the most. */
SELECT
s.name,
res.COUNT
FROM
species AS s
INNER JOIN (
SELECT
a.species_id AS id,
COUNT(*)
FROM
visits AS vi
INNER JOIN vets AS ve ON vi.vets_id = ve.id
INNER JOIN animalss AS a ON a.id = vi.animals_id
WHERE
ve.name = 'Maisy Smith'
GROUP BY
a.species_id
) AS res ON s.id = res.id
ORDER BY
res.COUNT DESC
LIMIT
1;