├── LICENSE ├── schema.sql ├── data.sql ├── README.md └── queries.sql /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 NOEL NOMGNE FOKA 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /schema.sql: -------------------------------------------------------------------------------- 1 | /* Database schema to keep the structure of entire database. */ 2 | 3 | CREATE TABLE animals ( 4 | id INTEGER PRIMARY KEY NOT NULL, 5 | name varchar, 6 | date_of_birth date, 7 | escape_attempts INTEGER, 8 | neutered boolean, 9 | weight_kg decimal 10 | ); 11 | 12 | ALTER TABLE animals ADD species varchar(255); 13 | 14 | CREATE TABLE owners ( 15 | id serial PRIMARY KEY, 16 | full_name VARCHAR(255), 17 | age INTEGER 18 | ); 19 | 20 | CREATE TABLE species ( 21 | id serial PRIMARY KEY, 22 | name VARCHAR(155) 23 | ); 24 | 25 | ALTER TABLE animals ADD CONSTRAINT animal_id UNIQUE (id); 26 | 27 | ALTER TABLE animals 28 | DROP species, 29 | ADD COLUMN species_id INTEGER, 30 | ADD COLUMN owner_id INTEGER; 31 | 32 | ALTER TABLE animals 33 | ADD CONSTRAINT fk_species FOREIGN KEY (species_id) REFERENCES species(id), 34 | ADD CONSTRAINT fk_owner FOREIGN KEY (owner_id) REFERENCES owners(id); 35 | 36 | create table vets( 37 | id int primary key generated always as identity, 38 | name varchar(50), 39 | age int, 40 | date_of_graduation date 41 | ); 42 | 43 | create table specializations ( 44 | species_id int, 45 | vet_id int, 46 | primary key(species_id, vet_id), 47 | constraint fk_species 48 | foreign key (species_id) 49 | references species(id), 50 | constraint fk_vets 51 | foreign key(vet_id) 52 | references vets(id) 53 | ); 54 | 55 | create table visits( 56 | animal_id int, 57 | vet_id int, 58 | date_of_visits date, 59 | constraint fk_animals 60 | foreign key(animal_id) 61 | references animals(id), 62 | constraint fk_vets 63 | foreign key(vet_id) 64 | references vets(id) 65 | ); -------------------------------------------------------------------------------- /data.sql: -------------------------------------------------------------------------------- 1 | /* Populate database with sample data. */ 2 | 3 | INSERT INTO animals(id, name, date_of_birth, escape_attempts, neutered, weight_kg) 4 | VALUES (1, 'Agumon', '2020-02-03', 0, true, 10.23); 5 | 6 | INSERT INTO animals(id, name, date_of_birth, escape_attempts, neutered, weight_kg) 7 | VALUES (2, 'Gabumon', '2018-11-15', 2, true, 8); 8 | 9 | INSERT INTO animals(id, name, date_of_birth, escape_attempts, neutered, weight_kg) 10 | VALUES (3, 'Pikachu', '2021-01-07', 1, false, 15.04); 11 | 12 | INSERT INTO animals(id, name, date_of_birth, escape_attempts, neutered, weight_kg) 13 | VALUES (4, 'Devimon', '2017-05-12', 5, true, 11); 14 | 15 | INSERT INTO animals (id, name, date_of_birth, escape_attempts, neutered, weight_kg) 16 | VALUES (5, 'Charmander', '2020-02-08', 0, FALSE, -11), 17 | (6, 'Plantmon', '2021-11-15', 2, TRUE, -5.7), 18 | (7, 'Squirtle', '1993-04-02', 3, FALSE, -12.13), 19 | (8, 'Angemon', '2005-06-12', 1, TRUE, -45), 20 | (9, 'Boarmon', '2005-06-07', 7, TRUE, 20.4), 21 | (10, 'Blossom', '1998-10-13', 3, TRUE, 17), 22 | (11, 'Ditto', '2022-05-14', 4, TRUE, 22) ; 23 | 24 | INSERT INTO owners (full_name, age) 25 | VALUES ('Sam Smith', 34), 26 | ('ennifer Orwell', 19), 27 | ('Bob', 45), 28 | ('Melody Pond', 77), 29 | ('Dean Winchester', 14), 30 | ('Jodie Whittaker', 38); 31 | 32 | INSERT INTO species (name) 33 | VALUES ('Pokemon'), ('Digimon'); 34 | 35 | UPDATE animals SET species_id = ( 36 | CASE 37 | WHEN name LIKE '%mon' THEN (SELECT id FROM species WHERE name = 'Digimon') 38 | ELSE (SELECT id FROM species WHERE name = 'Pokemon') 39 | END 40 | ); 41 | 42 | update animals set owner_id = case 43 | when name = 'Agumon' then 1 44 | when name in ('Gabumon', 'Pikachu') then 2 45 | when name in ('Devimon', 'Plantmon') then 3 46 | when name in ('Charmander', 'Squirtle', 'Blossom') then 4 47 | when name in ('Angemon', 'Boarmon') then 5 48 | end; 49 | 50 | insert into vets(name, age, date_of_graduation) values 51 | ('Vet William Tatcher', 45, '2000-04-23'), 52 | ('Vet Maisy Smith', 26, '2019-01-17'), 53 | ('Vet Stephanie Mendez', 64, '1981-05-04'), 54 | ('Vet Jack Harkness', 38, '2008-06-08'); 55 | 56 | insert into specializations(species_id, vet_id) values 57 | (1, 1), (1,3), (2,3), (2,4); 58 | 59 | insert into visits(animal_id,vet_id ,date_of_visits) values 60 | (1,1,'2020-05-24'), (1,3,'2020-07-22'), (2,4,'2021-02-02'), 61 | (3,2,'2020-01-05'), (3,2,'2020-03-08'), (3,2,'2020-05-14'), 62 | (4,3,'2021-05-04'), (5,4,'2021-02-24'), (6,2,'2019-12-21'), 63 | (6,1,'2020-08-10'), (6,2,'2021-04-07'), (7,3,'2019-09-29'), 64 | (8,4,'2020-10-03'), (8,4,'2020-11-04'), (9,2,'2019-01-24'), 65 | (9,2,'2019-05-15'), (9,2,'2020-02-27'), (9,2,'2020-08-03'), 66 | (10,3,'2020-05-24'), (10,1,'2021-01-11'); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Vet clinic database 2 | 3 | ## Getting Started 4 | 5 | This repository includes files with plain SQL that can be used to recreate a database: 6 | 7 | - Use [schema.sql](./schema.sql) to create all tables. 8 | - Use [data.sql](./data.sql) to populate tables with sample data. 9 | - Check [queries.sql](./queries.sql) for examples of queries that can be run on a newly created database. **Important note: this file might include queries that make changes in the database (e.g., remove records). Use them responsibly!** 10 | 11 | 12 | 13 | 34 | 35 | 36 | 37 | 38 | # 📗 Table of Contents 39 | 40 | - [Vet clinic database](#vet-clinic-database) 41 | - [Getting Started](#getting-started) 42 | - [📗 Table of Contents](#-table-of-contents) 43 | - [📖 Vet clinic database ](#-vet-clinic-database-) 44 | - [🛠 Built With SQL](#-built-with-sql) 45 | - [Tech Stack posgreSQL](#tech-stack-posgresql) 46 | - [Key Features ](#key-features-) 47 | - [💻 Getting Started ](#-getting-started-) 48 | - [Prerequisites](#prerequisites) 49 | - [Setup](#setup) 50 | - [Usage](#usage) 51 | - [👥 Author ](#-author-) 52 | - [🔭 Future Features ](#-future-features-) 53 | - [🤝 Contributing ](#-contributing-) 54 | - [⭐️ Show your support ](#️-show-your-support-) 55 | - [🙏 Acknowledgments ](#-acknowledgments-) 56 | - [📝 License ](#-license-) 57 | 58 | 59 | 60 | # 📖 Vet clinic database 61 | 62 | **Vet clinic database** is a... 63 | 64 | ## 🛠 Built With SQL 65 | 66 | ### Tech Stack posgreSQL 67 | 68 |
69 | Client 70 | 73 |
74 | 75 |
76 | Server 77 | 80 |
81 | 82 |
83 | Database 84 | 87 |
88 | 89 | 90 | 91 | ### Key Features 92 | 93 | 94 | - **SQL** 95 | - **Postgresql** 96 | 97 |

(back to top)

98 | 99 | 100 | 101 | ## 💻 Getting Started 102 | 103 | To get a local copy up and running, follow these steps. 104 | 105 | ### Prerequisites 106 | 107 | In order to run this project you need: 108 | 109 | - Install postgreSQL 110 | 111 | ### Setup 112 | 113 | Clone this repository to your desired folder: 114 | 115 | ```sh 116 | cd vet clinic 117 | git clone git@github.com:noelfoka/vet-clinic.git 118 | ``` 119 | 120 | ### Usage 121 | 122 | To run the project, execute the following command: 123 | 124 | ```sh 125 | psql 126 | ``` 127 | 128 |

(back to top)

129 | 130 | 131 | 132 | ## 👥 Author 133 | 134 | 👤 **Noel FOKA** 135 | 136 | - GitHub: [@noelfoka](https://github.com/noelfoka) 137 | - Twitter: [@noelnomgne](https://twitter.com/noelnomgne) 138 | - LinkedIn: [LinkedIn](https://linkedin.com/in/noelfoka) 139 | 140 |

(back to top)

141 | 142 | 143 | 144 | ## 🔭 Future Features 145 | 146 | > Describe 1 - 3 features you will add to the project. 147 | 148 | - [ ] **query and update animals table** 149 | - [ ] **query multiple tables** 150 | - [ ] **add 'join table' for visits** 151 | 152 |

(back to top)

153 | 154 | 155 | 156 | ## 🤝 Contributing 157 | 158 | Contributions, issues, and feature requests are welcome! 159 | 160 | Feel free to check the [issues page](../../issues/). 161 | 162 |

(back to top)

163 | 164 | 165 | 166 | ## ⭐️ Show your support 167 | 168 | If you like this project, give me a 🌟 169 | 170 |

(back to top)

171 | 172 | 173 | 174 | ## 🙏 Acknowledgments 175 | 176 | I would like to thank Microverse 177 | 178 |

(back to top)

179 | 180 | 181 | 182 | ## 📝 License 183 | 184 | This project is [MIT](./LICENSE) licensed. 185 | 186 |

(back to top)

187 | -------------------------------------------------------------------------------- /queries.sql: -------------------------------------------------------------------------------- 1 | /*Queries that provide answers to the questions from all projects.*/ 2 | 3 | SELECT * FROM animals WHERE name LIKE '%mon'; 4 | SELECT name FROM animals WHERE date_of_birth BETWEEN '2016-01-01' AND '2019-12-31'; 5 | SELECT name FROM animals WHERE neutered = true AND escape_attempts < 3; 6 | SELECT date_of_birth FROM animals WHERE name IN ('Agumon', 'Pikachu'); 7 | SELECT name, escape_attempts FROM animals WHERE weight_kg > 10.5; 8 | SELECT * FROM animals WHERE neutered = true; 9 | SELECT * from animals WHERE name != 'Gabumon'; 10 | SELECT * FROM animals WHERE weight_kg >= 10.4 AND weight_kg <= 17.3; 11 | 12 | /*update the animals table by setting the species column to unspecified.*/ 13 | 14 | BEGIN; 15 | UPDATE animals SET species = 'unspecified' ; 16 | 17 | SELECT * FROM animals ; 18 | ROLLBACK; 19 | 20 | SELECT * FROM animals ; 21 | 22 | BEGIN; 23 | UPDATE animals SET species = 'digimon' WHERE name LIKE '%mon'; 24 | UPDATE animals SET species = 'pokemon' WHERE species IS NULL; 25 | SELECT * FROM animals ; 26 | COMMIT; 27 | SELECT * FROM animals ; 28 | 29 | /*DELETE ALL RECORDS*/ 30 | 31 | BEGIN; 32 | DELETE FROM animals; 33 | SELECT * FROM animals ; 34 | ROLLBACK; 35 | SELECT * FROM animals ; 36 | 37 | /*INSIDE A TRANSACTION DELETE ANIMALS BORN AFTER JAN 1,2022*/ 38 | BEGIN; 39 | DELETE FROM animals WHERE date_of_birth > '2022-01-01'; 40 | SAVEPOINT ANBORNAFTERJAN12022; 41 | SELECT * FROM animals ; 42 | 43 | /*Update all animals' weight to be their weight multiplied by -1 THEN ROLLBACK TO SAVEPOINT*/ 44 | UPDATE animals SET weight_kg = weight_kg * -1 ; 45 | ROLLBACK TO ANIMALBORNAFTERJAN12022; 46 | SELECT * FROM animals ; 47 | 48 | /*Update all animals' weights that are negative to be their weight multiplied by -1.*/ 49 | UPDATE animals SET weight_kg = weight_kg * -1 WHERE weight_kg < 0; 50 | COMMIT; 51 | SELECT * FROM animals ; 52 | 53 | SELECT COUNT(*) AS animal_count FROM animals; 54 | SELECT COUNT(*) AS have_not_escaped FROM animals WHERE escape_attempts = 0; 55 | SELECT AVG(weight_kg) AS animals_average_weight FROM animals; 56 | SELECT neutered, SUM(escape_attempts) AS total_escaped_attempts FROM animals GROUP BY neutered; 57 | SELECT species, MIN(weight_kg) AS min_weight, MAX(weight_kg) AS max_weight FROM animals GROUP BY species; 58 | SELECT species, AVG(escape_attempts) AS average_escape_attempts FROM animals WHERE date_of_birth BETWEEN '1990-01-01' AND '2000-12-31' GROUP BY species; 59 | 60 | SELECT name FROM animals JOIN owners ON animals.owner_id = owners.id WHERE owners.full_name = 'Melody Pond'; 61 | 62 | SELECT animals.name FROM animals JOIN species ON animals.species_id = species.id WHERE species.name = 'Pokemon'; 63 | 64 | SELECT owners.full_name, COALESCE(array_agg(animals.name), '{}':: VARCHAR[]) AS animals_owned 65 | FROM owners 66 | LEFT JOIN animals ON owners.id = animals.owner_id 67 | GROUP BY owners.full_name; 68 | 69 | SELECT species.name AS species_name, COUNT(*) AS animal_count FROM animals JOIN species ON animals.species_id = species.id GROUP BY species.name; 70 | 71 | SELECT animals.name FROM animals JOIN species ON animals.species_id = species.id JOIN owners ON animals.owner_id = owners.id WHERE owners.full_name = 'Jennifer Orwell' AND species.name = 'Digimon'; 72 | 73 | SELECT animals.name FROM animals JOIN owners ON animals.owner_id = owners.id WHERE owners.full_name = 'Dean Winchester' AND animals.escape_attempts = 0; 74 | 75 | SELECT owners.full_name, COUNT(*) AS animal_count FROM owners JOIN animals ON owners.id = animals.owner_id GROUP BY owners.full_name ORDER BY animal_count DESC LIMIT 1; 76 | 77 | select a.name, vi.date_of_visits 78 | from animals a 79 | join visits vi on a.id = vi.animal_id 80 | join vets ve on ve.id = vi.vet_id 81 | where ve.name ='Vet William Tatcher' 82 | order by(vi.date_of_visits) desc limit 1; 83 | 84 | select count(distinct(a.name)) as total_seen 85 | from animals a 86 | join visits vi on a.id = vi.animal_id 87 | join vets ve on vi.vet_id = ve.id 88 | where ve.name = 'Vet Stephanie Mendez'; 89 | 90 | select ve.name, s.name as specialties 91 | from vets ve 92 | left join specializations sp on ve.id = sp.vet_id 93 | left join species s on s.id = sp.species_id; 94 | 95 | select a.name, vi.date_of_visits 96 | from animals a join visits vi on a.id = vi.animal_id 97 | join vets v on vi.vet_id = v.id 98 | where v.name = 'Vet Stephanie Mendez' 99 | and vi.date_of_visits between '2020-04-01' and '2020-08-30'; 100 | 101 | select a.name as most_visited, count(vi.animal_id) 102 | from animals a join visits vi on a.id = vi.animal_id 103 | join vets v on v.id = vi.vet_id 104 | group by(vi.animal_id, a.name) 105 | order by count(vi.animal_id) desc limit 1; 106 | 107 | select a.name, vi.date_of_visits as most_visited 108 | from animals a 109 | join visits vi on a.id = vi.animal_id 110 | join vets v on v.id = vi.vet_id 111 | where v.name = 'Vet Maisy Smith' 112 | order by(vi.date_of_visits) limit 1; 113 | 114 | select a.name,v.name as vet_name, vi.date_of_visits 115 | from animals a join visits vi on a.id = vi.animal_id 116 | join vets v on v.id = vi.vet_id 117 | order by(vi.date_of_visits) desc limit 1; 118 | 119 | select count(*) from visits 120 | join animals on animals.id = visits.animal_id 121 | join species on species.id = animals.species_id 122 | join vets on vets.id = visits.vet_id 123 | left join specializations sp on sp.vet_id = vets.id 124 | where sp.species_id != animals.species_id or sp.species_id is null; 125 | 126 | select species.name, count(species.id) 127 | from species join animals on species.id = animals.species_id 128 | join visits on visits.animal_id = animals.id 129 | join vets on vets.id = visits.vet_id 130 | where vets.name = 'Vet Maisy Smith' 131 | group by(species.id) 132 | order by(species.id) desc limit 1; --------------------------------------------------------------------------------