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

Feature4/add join visits table #11

Open
wants to merge 15 commits into
base: dev
Choose a base branch
from

Conversation

OmarRamoun
Copy link
Owner

✔️ Insert the following data for vets:

  • Vet William Tatcher is 45 years old and graduated Apr 23rd, 2000.
  • Vet Maisy Smith is 26 years old and graduated Jan 17th, 2019.
  • Vet Stephanie Mendez is 64 years old and graduated May 4th, 1981.
  • Vet Jack Harkness is 38 years old and graduated Jun 8th, 2008.

✔️ Insert the following data for specialties:

  • Vet William Tatcher is specialized in Pokemon.
  • Vet Stephanie Mendez is specialized in Digimon and Pokemon.
  • Vet Jack Harkness is specialized in Digimon.

✔️ Insert the following data for visits:

  • Agumon visited William Tatcher on May 24th, 2020.
  • Agumon visited Stephanie Mendez on Jul 22th, 2020.
  • Gabumon visited Jack Harkness on Feb 2nd, 2021.
  • Pikachu visited Maisy Smith on Jan 5th, 2020.
  • Pikachu visited Maisy Smith on Mar 8th, 2020.
  • Pikachu visited Maisy Smith on May 14th, 2020.
  • Devimon visited Stephanie Mendez on May 4th, 2021.
  • Charmander visited Jack Harkness on Feb 24th, 2021.
  • Plantmon visited Maisy Smith on Dec 21st, 2019.
  • Plantmon visited William Tatcher on Aug 10th, 2020.
  • Plantmon visited Maisy Smith on Apr 7th, 2021.
  • Squirtle visited Stephanie Mendez on Sep 29th, 2019.
  • Angemon visited Jack Harkness on Oct 3rd, 2020.
  • Angemon visited Jack Harkness on Nov 4th, 2020.
  • Boarmon visited Maisy Smith on Jan 24th, 2019.
  • Boarmon visited Maisy Smith on May 15th, 2019.
  • Boarmon visited Maisy Smith on Feb 27th, 2020.
  • Boarmon visited Maisy Smith on Aug 3rd, 2020.
  • Blossom visited Stephanie Mendez on May 24th, 2020.
  • Blossom visited William Tatcher on Jan 11th, 2021.

✔️ Write queries to answer the following:

  • Who was the last animal seen by William Tatcher?
  • How many different animals did Stephanie Mendez see?
  • List all vets and their specialties, including vets with no specialties.
  • List all animals that visited Stephanie Mendez between April 1st and August 30th, 2020.
  • What animal has the most visits to vets?
  • Who was Maisy Smith's first visit?
  • Details for most recent visit: animal information, vet information, and date of visit.
  • How many visits were with a vet that did not specialize in that animal's species?
  • What specialty should Maisy Smith consider getting? Look for the species she gets the most.

@OmarRamoun OmarRamoun added the enhancement New feature or request label Jun 19, 2022
@OmarRamoun OmarRamoun self-assigned this Jun 19, 2022
Copy link

@mateo951 mateo951 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @OmarRamoun,

Your project is complete! There is nothing else to say other than... it's time to merge it 🚀

Congratulations! 🎉

Cheers and Happy coding!👏👏👏

Feel free to leave any questions or comments in the PR thread if something is not 100% clear.


As described in the Code reviews limits policy you have a limited number of reviews per project (check the exact number in your Dashboard). If you think that the code review was not fair, you can request a second opinion using this form.

Comment on lines +62 to +80
CREATE TABLE vets (
vet_id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY NOT NULL,
name VARCHAR(255) NOT NULL,
age INT NOT NULL,
date_of_graduation DATE NOT NULL
);

/*
* There is a many-to-many relationship between the tables species and vets: a vet can specialize in multiple species, and a species can have multiple vets specialized in it. Create a "join table" called specializations to handle the relationship many-to-many relationship between the tables species and vets.
- id: integer (set it as autoincremented PRIMARY KEY)
- vet_id: integer (foreign key referencing vets table)
- species_id: integer (foreign key referencing species table)
*/

CREATE TABLE specializations (
specialization_id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY NOT NULL,
vet_id BIGINT REFERENCES vets(vet_id) ON DELETE CASCADE,
species_id BIGINT REFERENCES species(species_id) ON DELETE CASCADE
);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You added the new tables with the appropriate columns 🙌

Comment on lines +165 to +179
INSERT INTO vets
(full_name, age, graduation_date)
VALUES ('Vet William Tatcher', 45, DATE '2000-04-23');

Insert INTO vets
(full_name, age, graduation_date)
VALUES ('Vet Maisy Smith', 26, DATE '2019-01-17');

Insert INTO vets
(full_name, age, graduation_date)
VALUES ('Vet Stephanie Mendez', 64, DATE '1981-05-04');

INSERT INTO vets
(full_name, age, graduation_date)
VALUES ('Vet Jack Harkness', 38, DATE '2008-06-08');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done ✔

Comment on lines +159 to +207
SELECT owners.full_name, animals.name FROM owners
JOIN animals USING (owner_id)
WHERE animals.name = (SELECT animals.name FROM animals
JOIN visits USING (animal_id)
JOIN vets USING (vet_id)
WHERE vets.full_name = 'William Tatcher'
ORDER BY visits.date_of_visit DESC
LIMIT 1)
ORDER BY owners.full_name;

SELECT COUNT(DISTINCT animals.name) FROM animals
JOIN visits USING (animal_id)
JOIN vets USING (vet_id)
WHERE vets.full_name = 'Stephanie Mendez';

SELECT vets.full_name, vets.specialty FROM vets
JOIN visits USING (vet_id)
JOIN animals USING (animal_id)
WHERE EXTRACT(MONTH FROM visits.date_of_visit) >= 4 AND EXTRACT(MONTH FROM visits.date_of_visit) <= 8
AND EXTRACT(YEAR FROM visits.date_of_visit) >= 2020
GROUP BY vets.full_name, vets.specialty;

SELECT animals.name, visits.date_of_visit FROM animals
JOIN visits USING (animal_id)
JOIN vets USING (vet_id)
WHERE visits.date_of_visit = (SELECT visits.date_of_visit FROM visits
JOIN vets USING (vet_id)
JOIN animals USING (animal_id)
WHERE animals.name = 'Maisy Smith'
ORDER BY visits.date_of_visit DESC
LIMIT 1)
ORDER BY animals.name;

SELECT COUNT(*) FROM visits
JOIN vets USING (vet_id)
WHERE vets.specialty != (SELECT vets.specialty FROM vets
JOIN visits USING (vet_id)
JOIN animals USING (animal_id)
WHERE animals.name = 'Maisy Smith'
ORDER BY visits.date_of_visit DESC
LIMIT 1)
ORDER BY COUNT(*) DESC;

SELECT vets.specialty FROM vets
JOIN visits USING (vet_id)
JOIN animals USING (animal_id)
WHERE animals.name = 'Maisy Smith'
ORDER BY visits.date_of_visit DESC
LIMIT 1;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You answered all questions 🙌

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much @mateo951

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants