forked from Ochiengsteven/the-vet-clinic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.sql
127 lines (92 loc) · 2.94 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
-- animals with the names ending with mon
SELECT * FROM animals
WHERE name LIKE '%mon';
-- names of animals date_of_birth BETWEEN '2016-01-01' AND '2019-12-31'
SELECT name FROM animals
WHERE date_of_birth BETWEEN '2016-01-01' AND '2019-12-31';
-- names of animals that are neutered and have less than 3 escape attempts
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;
-- how many animals are there
SELECT COUNT(*) AS total_animals
FROM animals;
-- how many animals are there that never tried escaping
SELECT COUNT(*) AS non_escape_count
FROM animals
WHERE escape_attempts = 0;
-- avarage weight of animals
SELECT AVG(weight_kg) AS avg_weight
FROM animals;
-- Who escapes the most, neutered or not neutered animals?
SELECT name, escape_attempts AS max_escapes
FROM animals
WHERE escape_attempts = (SELECT MAX(escape_attempts) FROM animals);
-- The minimum and maximum weight of each type of animal.
SELECT species,
MIN(weight_kg) AS min_weight,
MAX(weight_kg) AS max_weight
FROM animals
WHERE species IN ('pokemon', 'digimon')
GROUP BY species;
-- Average number of escape attempts per animal type of those born between 1990 and 2000
SELECT species,
AVG(escape_attempts) AS average_escapes
FROM animals
WHERE species IN ('pokemon', 'digimon')
AND date_of_birth BETWEEN '1990-01-01' AND '2000-12-31'
GROUP BY species;
-- TRANSACTIONS
-- transaction to set species to unspecified
BEGIN TRANSACTION;
UPDATE animals
SET species = 'unspecified';
-- Verify the change was made
SELECT * FROM animals;
-- Roll back the transaction
ROLLBACK;
-- Verify that the species column is back to its original state
SELECT * FROM animals;
-- set species to pokemon and digimon
BEGIN TRANSACTION;
-- Update the species column to "digimon" for animals with names ending in "mon"
UPDATE animals
SET species = 'digimon'
WHERE name LIKE '%mon';
UPDATE animals
SET species = 'pokemon'
WHERE species IS NULL OR species = '';
SELECT * FROM animals;
COMMIT;
-- Verify that changes persist after the commit
SELECT * FROM animals;
-- Delete data transaction and rollbac
BEGIN TRANSACTION;
DELETE FROM animals;
ROLLBACK;
-- delete animals born after 2022 and update weight
BEGIN TRANSACTION;
-- Delete all animals born after Jan 1st, 2022
DELETE FROM animals
WHERE birthdate > '2022-01-01';
-- Create a savepoint for the transaction
SAVEPOINT before_update;
-- Update all animals' weight to be their weight multiplied by -1
UPDATE animals
SET weight = weight * -1;
-- Rollback to the savepoint
ROLLBACK TO before_update;
-- Update all animals' weights that are negative to be their weight multiplied by -1
UPDATE animals
SET weight = weight * -1
WHERE weight < 0;
COMMIT;