Learn SQL the Hard Way – Exercise 6 Select Across Many Tables

Here is my work for Exercise 6: Select Across Many Tables.

Exercise 6 code
Exercise 6 code

In this exercise, I learned how to select from multiple tables. Using the person, pet, and person_pet tables created previously, we can select all the pets owned by one person.

All the pets Zed owns
All the pets Zed owns

Extra Credit

  • This may be a mind blowing weird way to look at data if you already know a language like Python or Ruby. Take the time to model the same relationships using classes and objects then map it to this setup.
    In Ruby, person and pet would both be their own classes. A person has a first_name, last_name, id, and age. A pet has an id, name, age, and ‘dead’ status. To show that a person has-a pet, we would add a person_pet property to a person and specify the value as the pet they have.
  • Do a query that finds your pets you’ve added thus far.
    I’ve only added one pet, Drogon.

    Using conditions to find all the pets owned by the person named "Stacey"
    Using conditions to find all the pets owned by the person named “Stacey”

    The result, Zed's pet and mine
    The result, Zed’s pets and mine
  • Change the queries to use your person.id instead of the person.name like I’ve been doing.
Selecting by person id instead of name
Selecting by person id instead of name

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s