-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: dev
Are you sure you want to change the base?
Conversation
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.
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.
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 | ||
); |
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.
You added the new tables with the appropriate columns 🙌
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'); |
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.
Well done ✔
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; |
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.
You answered all questions 🙌
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.
Thank you so much @mateo951
✔️ Insert the following data for vets:
✔️ Insert the following data for specialties:
✔️ Insert the following data for visits:
✔️ Write queries to answer the following: