Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases

SQLite "Many-to-Many" Relationship, joining 2 tables with a 3º table for support. Someone can help me?

We will be writing ONLY the SQL query for this challenge. The library database contains a Media table with the columns media_id, title, img, format, year and category. It also contains a Genres table with the columns genre_id and genre. To join these tables, there is a Media_Genres table that contains the column media_id and genre_id Add to the following SELECT statement to JOIN the Media table and the Genres table using the joining table Media_Genres. SELECT * FROM Media WHERE media_id=3; NOTE: You will need to add the table to the WHERE clause so that the media_id column is not ambiguous.

Someone can explain for me, how this work? i don't understand how i compare the values ​​with the third column

1 Answer

Steven Parker
Steven Parker
229,005 Points

Did you mean "third table" instead of "third column"?

Perhaps a simplified example may help:

Let's say we have a list of programmers in a table named coders that has columns for id and name.

We also have a table of knowlege areas named skills that has columns id and language.

Now, since each programmer may know several languages, and more than one programmer will likely know each language, there is a many-to-many relationship between them. To indicate this relationship, there is a third table (sometimes known as a junction table), that has a coder id and a skill id for each row. This table is named coder_skills to indicate its usage.

So if we wanted to see all languages that Joe knows, we could write this query:

SELECT language FROM skills
  JOIN coder_skills ON skills.id = skill_id
  JOIN coders ON coder_id = coders.id
  WHERE name = "Joe";

Or, to find all programmers that know Python:

SELECT name from coders
  JOIN coder_skills ON coders.id = coder_id
  JOIN skills ON skill_id = skills.id
  WHERE language = "Python";

Does it make more sense now?