generated from microverseinc/curriculum-template-databases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.sql
238 lines (184 loc) · 6.52 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
/*Queries that provide answers to the questions from all projects.*/
SELECT * FROM animals WHERE name LIKE '%mon%';
SELECT name FROM animals WHERE date_of_birth BETWEEN '2016-01-01' AND '2019-01-01';
SELECT name FROM animals WHERE neutered IS 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 IS TRUE;
SELECT * FROM animals WHERE name <> 'Gabumon';
SELECT * FROM animals WHERE weight_kg >= 10.04 AND weight_kg <= 17.03;
/*Transaction Queries */
-- 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 the species columns went back to the state before the transaction.
BEGIN TRANSACTION;
UPDATE animals
SET species = 'unspecified';
SELECT name, species FROM animals;
ROLLBACK;
SELECT * FROM animals;
--Update the animals table by setting the species column to digimon for all animals that have a name ending in mon.
--Update the animals table by setting the species column to pokemon for all animals that don't have species already set.
--Commit the transaction.
--Verify that change was made and persists after commit.
BEGIN TRANSACTION;
UPDATE animals
SET species = 'digimon'
WHERE name LIKE '%mon%';
UPDATE animals
SET species = 'pokemon'
WHERE species IS NULL;
COMMIT;
SELECT * FROM animals;
--delete all records in the animals table, then roll back the transaction.
--After the rollback verify if all records in the animals
BEGIN TRANSACTION;
DELETE FROM animals;
ROLLBACK;
SELECT * FROM animals;
--Delete all animals born after Jan 1st, 2022.
--Create a savepoint for the transaction.
--Update all animals' weight to be their weight multiplied by -1.
--Rollback to the savepoint
--Update all animals' weights that are negative to be their weight multiplied by -1.
--Commit transaction
BEGIN TRANSACTION;
DELETE FROM animals
WHERE date_of_birth > '2022-01-01';
SAVEPOINT SP1;
UPDATE animals
SET weight_kg = weight_kg * -1;
ROLLBACK TO SP1;
UPDATE animals
SET weight_kg = weight_kg * -1
WHERE weight_kg < 0;
COMMIT;
/* Queries */
--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
neutered, COUNT(escape_attempts)
FROM
animals
WHERE escape_attempts > 0
GROUP BY neutered;
--What is the minimum and maximum weight of each type of animal?
SELECT
species, MIN(weight_kg)
FROM
animals
GROUP BY species;
SELECT
species, 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 >= '1990-01-01' AND date_of_birth <= '2000-01-01'
GROUP BY species;
/* JOIN Queries */
SELECT name, full_name
FROM animals A
JOIN owners O ON O.Id = A.owner_id
WHERE O.full_name = 'Melody Pond';
SELECT A.name as animal_name, S.name as type
FROM animals A
JOIN species S ON S.id = A.species_id
WHERE S.name = 'Pokemon';
SELECT name, full_name
FROM animals A
FULL OUTER JOIN owners O
ON O.id = A.owner_id;
SELECT A.name as animal_name, S.name as type
FROM animals A
FULL OUTER JOIN species S
ON S.id = A.species_id;
SELECT A.name as animal_name, S.name as type, O.full_name as owner_name
FROM animals A
JOIN species S ON S.id = A.species_id
JOIN owners O ON O.id = A.owner_id
WHERE O.full_name = 'Jennifer Orwell' AND S.name = 'Digimon';
SELECT name, full_name
FROM animals A
JOIN owners O ON O.id = A.owner_id
WHERE O.full_name = 'Dean Winchester' AND escape_attempts = 0;
SELECT O.full_name as owner_name, COUNT(A.*) as animal_quantity
FROM animals A
FULL OUTER JOIN owners O
ON O.id = A.owner_id
GROUP BY owner_name
ORDER BY animal_quantity DESC;
-- QUERIES for join table
SELECT A.name as animal_name, V.name as vet_name, D.date_of_visit as visit_date
FROM visits D
JOIN vets V ON V.id = D.vet_id
JOIN animals A ON A.id = D.animals_id
WHERE V.id = 1
ORDER BY date_of_visit DESC
LIMIT 1;
SELECT COUNT(A.name) as animals, V.name as vet_name
FROM visits D
JOIN vets V ON V.id = D.vet_id
JOIN animals A ON A.id = D.animals_id
WHERE V.id = 3
GROUP BY V.name;
SELECT V.name as vet_name, S.name as specialties
FROM specializations A
FULL OUTER JOIN vets V ON V.id = A.vet_id
FULL OUTER JOIN species S ON S.id = A.species_id;
SELECT A.name as animal_name, V.name as vet_name, D.date_of_visit
FROM visits D
JOIN vets V ON V.id = D.vet_id
JOIN animals A ON A.id = D.animals_id
WHERE V.id = 3 AND D.date_of_visit BETWEEN '2020-04-01' AND '2020-08-30';
SELECT A.name as animal_name, COUNT(V.*) as visits
FROM visits V
JOIN animals A ON A.id = V.animals_id
GROUP BY animal_name
ORDER BY visits DESC
LIMIT 1;
SELECT A.name as animal_name, V.name as vet_name, D.date_of_visit
FROM visits D
JOIN animals A ON A.id = D.animals_id
JOIN vets V ON V.id = D.vet_id
WHERE V.id = 2
ORDER BY D.date_of_visit ASC
LIMIT 1;
SELECT D.date_of_visit, A.name as animal_name, A.date_of_birth, A.escape_attempts, A.weight_kg, V.name as vet_name, V.age,V.date_of_graduation
FROM visits D
JOIN animals A ON A.id = D.animals_id
JOIN vets V ON V.id = D.vet_id
ORDER BY D.date_of_visit DESC
LIMIT 1;
SELECT A.name as animal_name, Z.name, V.name, V.age, S.species_id
FROM animals A
FULL JOIN visits B ON B.animals_id = A.id
FULL JOIN species Z ON A.species_id = Z.id
FULL JOIN vets V ON V.id = B.vet_id
FUll JOIN specializations S ON S.vet_id = V.id
WHERE S.species_id is null
or S.species_id != A.species_id AND V.id != 3;
SELECT Z.name as species, COUNT(A.*) as animal_frec
FROM animals A
FULL JOIN visits B ON B.animals_id = A.id
FULL JOIN species Z ON A.species_id = Z.id
FULL JOIN vets V ON V.id = B.vet_id
FUll JOIN specializations S ON S.vet_id = V.id
WHERE V.name = 'Maisy Smith'
GROUP BY species
ORDER BY animal_frec DESC
LIMIT 1;
SELECT COUNT(*) FROM visits where animal_id = 4;
SELECT * FROM visits where vet_id = 2;
SELECT * FROM owners where email = 'owner_18327@mail.com';
-- Indexing to improve performance
CREATE INDEX animal_id_index ON visits (animals_id);
CREATE INDEX vets_id_index ON visits (vets_id);
CREATE INDEX email_index ON owners (email ASC);