Skip to content
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

Pull Request for using indexing in vet_clinic #6

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 10 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
# curriculum-databases-projects-template

> This template should be used for database related projects at Microverse.
> Generate your own repository, update this README and edit all files content while working on projects. You should not be adding any new files unless asked otherwise.
feature_branch_day_4_SQL
> This is a project showing data of animals in a vet-clinic. Different queries can be made using SQL commands. In this branch more new tables,data are added.Queries are made utilising JOIN. Many to many relationships are established. Different queries are made utilising different types of JOIN.


## Getting Started

This repository includes files with plain SQL that can be used to recreate a database:
This repository includes 3 files with plain SQL that can be used to recreate a database:

- Use [schema.sql](./schema.sql) to create all tables.
- Use [data.sql](./data.sql) to populate tables with sample data.
- 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!**
- schema.sql which creates all tables.
- data.sql to populate tables with sample data.
- queries.sql for examples of queries that can be run on a newly created database using JOIN.


## Authors

👤 **Author1**
👤 **Tanusri Ghosh**

- GitHub: [@githubhandle](https://github.com/githubhandle)
- Twitter: [@twitterhandle](https://twitter.com/twitterhandle)
- LinkedIn: [LinkedIn](https://linkedin.com/in/linkedinhandle)

👤 **Author2**

- GitHub: [@githubhandle](https://github.com/githubhandle)
- Twitter: [@twitterhandle](https://twitter.com/twitterhandle)
- LinkedIn: [LinkedIn](https://linkedin.com/in/linkedinhandle)
- GitHub: https://github.com/chuaindia)
- Twitter: https://twitter.com/chuaghosh@
- LinkedIn:https://linkedin.com/in/tanusrighosh

## 🤝 Contributing

Expand Down
121 changes: 118 additions & 3 deletions data.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,120 @@
/* Populate database with sample data. */

INSERT INTO animals (name) VALUES ('Luna');
INSERT INTO animals (name) VALUES ('Daisy');
INSERT INTO animals (name) VALUES ('Charlie');
INSERT INTO animals (name, date_of_birth, escape_attempts, neutered, weight_kg) VALUES
('Agumon', '02-03-2020', 0, 'True', 10.23),
('Gabumon', '11-15-2018', 2, 'True', 8),
('Pikachu', '01-07-2021', 1, 'False', 15.04),
('Devimon', '05-12-2017', 5, 'True', 11);

/* Day2 */

INSERT INTO animals (name, date_of_birth, escape_attempts, neutered, weight_kg) VALUES
('Charmander', '02-08-2020', 0, 'False', -11),
('Plantmon', '11-15-2021', 2, 'True', -5.7),
('Squirtle', '04-02-1993', 3, 'False', -12.13),
('Angemon', '06-12-2005', 1, 'True', -45),
('Boarmon', '06-07-2005', 7, 'True', 20.4),
('Blossom', '10-13-1998', 3, 'True', 17),
('Ditto', '05-14-2022', 4, 'True', 22);

/* Day3 */

INSERT INTO owners (full_name, age)
VALUES
('Sam Smith', 34),
('Jennifer Orwell', 19),
('Bob', 45),
('Melody Pond', 77),
('Dean Winchester', 14),
('Jodie Whittaker', 38);

INSERT INTO species (name)
VALUES
('Pokemon'),
('Digimon');

UPDATE animals SET species_id = 1 WHERE name LIKE '%mon';
UPDATE animals SET species_id = 2 WHERE name NOT LIKE '%mon';

UPDATE animals SET owner_id = 1 WHERE name IN ('Agumon');
UPDATE animals SET owner_id = 2 WHERE name IN ('Gabumon','Pikachu');
UPDATE animals SET owner_id = 3 WHERE name IN ('Devimon','Plantmon');
UPDATE animals SET owner_id = 4 WHERE name IN ('Charmander','Squirtle','Blossom');
UPDATE animals SET owner_id = 5 WHERE name IN ('Angemon','Boarmon');

/* Day 4 */

INSERT INTO vets (name, age, date_of_graduation) VALUES
('William Tatcher', 45, '04-23-2000'),
('Maisy Smith', 26, '01-17-2019'),
('Stephanie Mendez', 64, '05-04-1981'),
('Jack Harkness', 38, '06-08-2008');

INSERT INTO specializations (vets_id, species_id) VALUES
(1, 1),
(3, 2),
(3, 1),
(4, 2);

INSERT INTO visits (animals_id, vets_id, date_of_visit) VALUES
(1, 1, '05-24-2020'),
(1, 3, '07-22-2020'),
(2, 4, '02-02-2021'),
(3, 2, '01-05-2020'),
(3, 2, '03-08-2020'),
(3, 2, '05-14-2020'),
(4, 3, '05-04-2021'),
(5, 4, '02-24-2021'),
(6, 2, '12-21-2019'),
(6, 1, '08-10-2020'),
(6, 2, '04-07-2021'),
(7, 3, '09-29-2019'),
(8, 4, '10-03-2020'),
(8, 4, '11-04-2020'),
(9, 2, '01-24-2019'),
(9, 2, '05-15-2019'),
(9, 2, '02-27-2020'),
(9, 2, '08-03-2020'),
(10, 3, '05-24-2020'),
(10, 1, '01-11-2021');


/* day 5, 2nd week -- Pair Programming */


-- This will add 3.594.280 visits considering you have 10 animals, 4 vets, and it will use around ~87.000 timestamps (~4min approx.)
INSERT INTO visits (animal_id, vet_id, date_of_visit) SELECT * FROM (SELECT id FROM animals) animal_ids, (SELECT id FROM vets) vets_ids, generate_series('1980-01-01'::timestamp, '2021-01-01', '4 hours') visit_timestamp;

-- This will add 2.500.000 owners with full_name = 'Owner <X>' and email = 'owner_<X>@email.com' (~2min approx.)
insert into owners (full_name, email) select 'Owner ' || generate_series(1,2500000), 'owner_' || generate_series(1,2500000) || '@mail.com';

/* Execution Time 216.905 ms */


-- This will add 3.594.280 visits considering you have 10 animals, 4 vets, and it will use around ~87.000 timestamps (~4min approx.)
INSERT INTO visits (animal_id, vet_id, date_of_visit) SELECT * FROM (SELECT id FROM animals) animal_ids, (SELECT id FROM vets) vets_ids, generate_series('1980-01-01'::timestamp, '2021-01-01', '4 hours') visit_timestamp;

-- This will add 2.500.000 owners with full_name = 'Owner <X>' and email = 'owner_<X>@email.com' (~2min approx.)
insert into owners (full_name, email) select 'Owner ' || generate_series(1,2500000), 'owner_' || generate_series(1,2500000) || '@mail.com';

/* Execution Time 435.957 ms */


-- This will add 3.594.280 visits considering you have 10 animals, 4 vets, and it will use around ~87.000 timestamps (~4min approx.)
INSERT INTO visits (animal_id, vet_id, date_of_visit) SELECT * FROM (SELECT id FROM animals) animal_ids, (SELECT id FROM vets) vets_ids, generate_series('1980-01-01'::timestamp, '2021-01-01', '4 hours') visit_timestamp;

-- This will add 2.500.000 owners with full_name = 'Owner <X>' and email = 'owner_<X>@email.com' (~2min approx.)
insert into owners (full_name, email) select 'Owner ' || generate_series(1,2500000), 'owner_' || generate_series(1,2500000) || '@mail.com';


/* Execution Time 654.253 ms */

-- This will add 3.594.280 visits considering you have 10 animals, 4 vets, and it will use around ~87.000 timestamps (~4min approx.)
INSERT INTO visits (animal_id, vet_id, date_of_visit) SELECT * FROM (SELECT id FROM animals) animal_ids, (SELECT id FROM vets) vets_ids, generate_series('1980-01-01'::timestamp, '2021-01-01', '4 hours') visit_timestamp;

-- This will add 2.500.000 owners with full_name = 'Owner <X>' and email = 'owner_<X>@email.com' (~2min approx.)
insert into owners (full_name, email) select 'Owner ' || generate_series(1,2500000), 'owner_' || generate_series(1,2500000) || '@mail.com';


/* Execution Time 1009.139 ms */

194 changes: 193 additions & 1 deletion queries.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,195 @@
/*Queries that provide answers to the questions from all projects.*/

SELECT * from animals WHERE name = 'Luna';
SELECT * from animals WHERE name = '%mon';
SELECT * FROM animals WHERE date_of_birth BETWEEN '2016-01-01' AND '2019-12-31';
SELECT * 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 NOT IN ('Gabumon');
SELECT * FROM animals WHERE weight_kg BETWEEN 10.4 AND 17.3;

/* Day 2 */

/* Transaction 1 */

BEGIN;

UPDATE animals SET species='unspecified';

SELECT species FROM animals;
ROLLBACK;

SELECT species FROM animals;

UPDATE animals SET species='digimon' WHERE name LIKE '%mon';
UPDATE animals SET species='pokemon' WHERE species NOT LIKE '%mon';

COMMIT;

SELECT * FROM animals;

/* Transaction 2 */

BEGIN;

DELETE FROM animals;

ROLLBACK;

/* Transaction 3 */

BEGIN;

SAVEPOINT SP1;

DELETE FROM animals WHERE date_of_birth > '2022-01-01';

SAVEPOINT SP2;

UPDATE animals SET weight_kg = weight_kg * -1;

ROLLBACK TO SP2;

UPDATE animals SET weight_kg = weight_kg * -1 WHERE weight_kg < 0;

COMMIT;

SELECT * FROM animals;

/* Answer of the Questions */

SELECT COUNT(*) FROM animals;
SELECT COUNT(*) FROM animals WHERE escape_attempts = 0;
SELECT AVG(weight_kg) FROM animals;
SELECT neutered, MAX(escape_attempts) FROM animals GROUP BY neutered;
SELECT species, MIN(weight_kg) as Minimum, MAX(weight_kg) as Maximum FROM animals GROUP BY species;
SELECT AVG(escape_attempts) FROM animals WHERE date_of_birth BETWEEN '1990-01-01' AND '2000-12-31' GROUP BY species;


/* Day3 */

SELECT name FROM animals
JOIN owners ON owners.id = animals.owner_id
WHERE owners.full_name = 'Melody Pond';

SELECT animals.name FROM animals
JOIN species ON species.id = animals.species_id
WHERE species.name = 'Pokemon';

SELECT name AS name_of_animals, full_name AS name_of_owners FROM owners
LEFT JOIN animals ON owners.id = animals.owner_id;

SELECT species.name, COUNT(*) FROM animals
LEFT JOIN species ON species.id = animals.species_id
GROUP BY species.name;

SELECT animals.name FROM animals
JOIN species ON species.id = animals.species_id
JOIN owners ON owners.id = animals.owner_id
WHERE species.name = 'Digimon' AND owners.full_name = 'Jennifer Orwell' ;

SELECT name FROM animals
JOIN owners ON owners.id = animals.owner_id
WHERE owners.full_name = 'Dean Winchester' AND animals.escape_attempts = 0;

SELECT full_name, COUNT(full_name) AS maximum FROM animals
JOIN owners ON owners.id = animals.owner_id
GROUP BY owners.full_name
ORDER BY maximum
DESC LIMIT 1;


/* Day 4 */

SELECT animals.name FROM visits
JOIN animals on animals.id = visits.animals_id
JOIN vets on vets.id = visits.vets_id
WHERE vets.name = 'William Tatcher'
ORDER BY date_of_visit
DESC LIMIT 1;

SELECT vets.name, COUNT(*) FROM visits
JOIN animals on animals.id = visits.animals_id
JOIN vets on vets.id = visits.vets_id
WHERE vets.name = 'Stephanie Mendez'
GROUP BY vets.name;

SELECT vets.name, species.name FROM vets
LEFT JOIN specializations on vets.id = specializations.vets_id
LEFT JOIN species on species.id = specializations.species_id;

SELECT animals.name FROM visits
JOIN vets on vets.id = visits.vets_id
JOIN animals on animals.id = visits.animals_id
WHERE vets.name = 'Stephanie Mendez'
AND date_of_visit BETWEEN '04-01-2020' AND '08-30-2020';

SELECT animals.name, COUNT(*) AS number_of_visits FROM visits
JOIN vets on vets.id = visits.vets_id
JOIN animals on animals.id = visits.animals_id
GROUP BY animals.name
ORDER BY number_of_visits
DESC LIMIT 1;

SELECT animals.name FROM visits
JOIN vets on vets.id = visits.vets_id
JOIN animals on animals.id = visits.animals_id
WHERE vets.name = 'Maisy Smith'
ORDER BY date_of_visit
LIMIT 1;

SELECT animals.name, vets.name, date_of_visit FROM visits
JOIN vets on vets.id = visits.vets_id
JOIN animals on animals.id = visits.animals_id
ORDER BY date_of_visit
DESC LIMIT 1;

SELECT COUNT(*) FROM vets
LEFT JOIN visits on vets.id = visits.vets_id
LEFT JOIN specializations on vets.id = specializations.vets_id
LEFT JOIN species on species.id = specializations.species_id
WHERE species.name is NULL;

SELECT species.name, COUNT(*) FROM visits
JOIN vets on vets.id = visits.vets_id
JOIN animals on animals.id = visits.animals_id
JOIN species on species.id = animals.species_id
WHERE vets.name = 'Maisy Smith'
GROUP BY species.name
LIMIT 1;


/* day 5, 2nd week -- Pair Programming */

explain analyze SELECT COUNT(*) FROM visits where animal_id = 4;

/* Execution Time 216.905 ms */


explain analyze SELECT COUNT(*) FROM visits where animal_id = 4;

/* Execution Time 435.957 ms */

explain analyze SELECT COUNT(*) FROM visits where animal_id = 4;

/* Execution Time 654.253 ms */

explain analyze SELECT COUNT(*) FROM visits where animal_id = 4;

/* Execution Time 1009.139 ms */

EXPLAIN ANALYZE SELECT * FROM visits where vet_id = 2;

/* Execution Time 2085.938 ms */

EXPLAIN ANALYZE SELECT * FROM owners where email = '[email protected]';

/* Execution Time 814.106 ms */







Loading