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!

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


SQL Joins with three tables

These are the instructions: 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.

NOTE: You will need to add the table to the WHERE clause so that the media_id column is not ambiguous.

This is the best I could come up with: select Media.media_id, title, img, format, year, category, Genres.genre_id, Genres.genre FROM Media Join Media_Genres ON Media.media_id = Media_Genres.media_id Left Outer Join Genres ON Media_Genres.genre_id = Genres.genre_id;

There is mention of the where clause and yet, there doesn't seem to be a value provided to fill the where clause. I've tried several different variations and I can' seem to get my queries to return the full table with genres and Genre_ids.

I've decided that the real issue with this challenge is that there is clearly information missing to complete it.

Handy Metellus
Handy Metellus
7,796 Points

I'm having the same issue.

This is what I came up with but it is asking for a where clause which we shouldn’t need. Thanks

select m., g. from media as m join media_genres as mg on m.media_id = mg.media_id left join genres as g on g.genre_id = mg.genre_id;

2 Answers

anil rahman
anil rahman
7,786 Points
----------------select everything---------------
FROM Media 
----------------do the joins----------------------
JOIN Media_Genres ON Media.media_id = Media_Genres.media_id 
JOIN Genres ON Media_Genres.genre_id = Genres.genre_id 
 --------------where part for the ambiguous media_id-------------------------
WHERE Media_Genres.media_id = 3;

Thank you, the where clause did the trick.

anil rahman
anil rahman
7,786 Points

Can you not link this challenge?