-
Notifications
You must be signed in to change notification settings - Fork 0
Vet clinic database: query and update table #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,25 @@ | ||
| SET datestyle = dmy; | ||
|
|
||
| INSERT INTO animals(id, name, date_of_birth, escape_attempts, neutered, weight_kg) | ||
| VALUES(1, 'Agumon','02/03/2020',0,true,10.23); | ||
| INSERT INTO animals(id, name, date_of_birth, escape_attempts, neutered, weight_kg) | ||
| VALUES(2, 'Gabumon','15/11/2018',2,true,8.0); | ||
| INSERT INTO animals(id, name, date_of_birth, escape_attempts, neutered, weight_kg) | ||
| VALUES(3, 'Pikachu','07/01/2021',1,false,15.04); | ||
| INSERT INTO animals(id, name, date_of_birth, escape_attempts, neutered, weight_kg) | ||
| VALUES(4, 'Devimon','12/05/2017',5,true,11.0); | ||
| INSERT INTO animals(name, date_of_birth, escape_attempts, neutered, weight_kg) | ||
| VALUES('Agumon','02/03/2020',0,true,10.23); | ||
|
|
||
| INSERT INTO animals(name, date_of_birth, escape_attempts, neutered, weight_kg) | ||
| VALUES('Gabumon','15/11/2018',2,true,8.0); | ||
| INSERT INTO animals(name, date_of_birth, escape_attempts, neutered, weight_kg) | ||
| VALUES('Pikachu','07/01/2021',1,false,15.04); | ||
| INSERT INTO animals(name, date_of_birth, escape_attempts, neutered, weight_kg) | ||
| VALUES('Devimon','12/05/2017',5,true,11.0); | ||
| INSERT INTO animals(name, date_of_birth, escape_attempts, neutered, weight_kg) | ||
| VALUES('Charmander','08/02/2020',0,false,-11.0); | ||
| INSERT INTO animals(name, date_of_birth, escape_attempts, neutered, weight_kg) | ||
| VALUES('Plantmon','15/11/2021',2,true,-5.7); | ||
| INSERT INTO animals(name, date_of_birth, escape_attempts, neutered, weight_kg) | ||
| VALUES('Squirtle','02/04/1993',3,false,-12.13); | ||
| INSERT INTO animals(name, date_of_birth, escape_attempts, neutered, weight_kg) | ||
| VALUES('Angemon','12/06/2005',1,true,-45.0); | ||
| INSERT INTO animals(name, date_of_birth, escape_attempts, neutered, weight_kg) | ||
| VALUES('Boarmon','07/06/2005',7,true,20.4); | ||
| INSERT INTO animals(name, date_of_birth, escape_attempts, neutered, weight_kg) | ||
| VALUES('Blossom','13/10/1998',3,true,17.0); | ||
| INSERT INTO animals(name, date_of_birth, escape_attempts, neutered, weight_kg) | ||
| VALUES('Ditto','14/05/2022',4,true,22.0); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,4 +24,44 @@ SELECT * FROM animals | |
| WHERE name != 'Gabumon'; | ||
|
|
||
| SELECT * FROM animals | ||
| WHERE weight_kg >= 10.4 AND weight_kg <= 17.3 | ||
| WHERE weight_kg >= 10.4 AND weight_kg <= 17.3 | ||
|
|
||
| -- queries to perform transaction | ||
|
|
||
| BEGIN; | ||
| UPDATE animals SET species = 'unspecified'; | ||
| ROLLBACK; | ||
|
|
||
| BEGIN; | ||
| UPDATE animals SET species = 'digimon' WHERE name like '%mon'; | ||
| UPDATE animals SET species = 'pokemon'WHERE species is null; | ||
| COMMIT; | ||
|
|
||
| BEGIN; | ||
| DELETE FROM animals; | ||
| ROLLBACK; | ||
|
|
||
| BEGIN; | ||
| DELETE FROM animals WHERE date_of_birth > '01/07/2022'; | ||
| SAVEPOINT savepoint_1; | ||
| UPDATE animals SET weight_kg = (weight_kg * -1); | ||
| ROLLBACK TO savepoint_1; | ||
| UPDATE animals SET weight_kg = (weight_kg * - 1) WHERE weight_kg < 0; | ||
| COMMIT; | ||
|
|
||
| -- queries to perform aggregate functions | ||
|
|
||
| SELECT COUNT(*) FROM animals; | ||
|
|
||
|
Comment on lines
+56
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| SELECT COUNT(*) FROM animals WHERE escape_attempts = 0; | ||
|
|
||
| SELECT AVG(weight_kg) FROM animals | ||
|
|
||
|
Comment on lines
+60
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| SELECT name FROM animals | ||
| ORDER BY escape_attempts DESC LIMIT 1; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Please remember to update the screenshot when you are done. 👍 |
||
|
|
||
| SELECT MIN(weight_kg), MAX(weight_kg) FROM animals GROUP BY(species); | ||
|
|
||
|
Comment on lines
+66
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| SELECT ROUND(AVG(escape_attempts),2) AS average FROM animals | ||
| WHERE EXTRACT(YEAR FROM date_of_birth) BETWEEN '1990' AND '2000' | ||
| GROUP BY(species) ORDER BY average; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,12 @@ | ||
| /* Database schema to keep the structure of entire database. */ | ||
| CREATE TABLE animals( | ||
| id INTEGER NOT NULL PRIMARY KEY, | ||
| id SERIAL NOT NULL PRIMARY KEY, | ||
| name VARCHAR(255) NOT NULL, | ||
| date_of_birth DATE NOT NULL, | ||
| escape_attempts integer NOT NULL, | ||
| neutered BOOLEAN NOT NULL, | ||
| weight_kg FLOAT NOT NULL | ||
| ); | ||
| ); | ||
|
|
||
| ALTER TABLE animals | ||
| ADD COLUMN species VARCHAR(255); |

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.