Let's do the challenge associated with the JOIN clause entitled, "Bobby's Hobbies".
Instructions:
Step 1We've created a database of people and hobbies, and each row in hobbies is related to a row in persons via the person_id column. In this first step, insert one more row in persons and then one more row in hobbies that is related to the newly inserted person. I inserted the first female character that came to mind in one of my favorite romantic sitcoms.
Code:
INSERT INTO persons (name, age) VALUES ("Robin Scherbattsky", 32);
INSERT INTO hobbies (person_id, name) VALUES (6, "reporting");
Step 2
Now, select the 2 tables with a join so that you can see each person's name next to their hobby.
Code:
SELECT persons.name, hobbies.name FROM persons JOIN hobbies ON persons.id = hobbies.person_id;
***A more better code is:
SELECT persons.name AS person, hobbies.name AS hobby FROM persons JOIN hobbies ON persons.id = hobbies.person_id;
but it would not let me go to the next step so we go with the previous code.***
Step 3
Now, add an additional query that shows only the name and hobbies of 'Bobby McBobbyFace', using JOIN combined with WHERE.
Code:
SELECT persons.name, hobbies.name FROM persons JOIN hobbies ON persons.id = hobbies.person_id WHERE persons.name = "Bobby McBobbyFace";
Since we were able to know what JOIN clause meant and how it works, this challenge has been done quicker than expected.
###
Comments
Post a Comment