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

Peter Waters
Peter Waters
13,327 Points

Cannot get this join to work

I can't get this MySQL query to work. It's probably something really obvious, but if someone could point me in the right direction I'd really appreciate it!

The question was:

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.

The query

SELECT * FROM Media JOIN Media_Genres ON Media.media_id = Media_Genres.media_id JOIN Genres ON Media_Genres.genre_id = Genres.genre_id WHERE Media.media_id = 1;

5 Answers

anil rahman
anil rahman
7,786 Points

I don't see anything wrong with your syntax. I don't see why you have the where clause, don't see it being asked for in the question.

Peter Waters
Peter Waters
13,327 Points

Hi John,

I've just tried this challenge again and it's a bit confusing. Here's the full working query:

SELECT * FROM Media JOIN Media_Genres ON Media.media_id = Media_Genres.media_id JOIN Genres ON Media_Genres.genre_id = Genres.genre_id WHERE Media_Genres.media_id = 3;

Basically the value of media_id has to be 3, otherwise the query fails. If you run the query without the WHERE clause you'll see that the only results have an id of 3. If you use any other value you get the error Bummer! Nothing needs to be removed, you need to add a JOIN for the Genres ON the genre_id column in both the Media and Genres tables.

I ended up figuring it out... this ended up working for me:

SELECT * FROM Media JOIN Media_Genres ON Media.media_id = Media_Genres.media_id JOIN Genres ON Genres.genre_id = Media_Genres.genre_id WHERE Media_Genres.media_id = Media.media_id

Peter Waters
Peter Waters
13,327 Points

Sorry, I left out the bit of the question that says:

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

I managed to solve it. The WHERE clause should have WHERE Media_Genres.media_id = 1 not WHERE Media.media_id = 1

Thanks for your reply!

Hey Peter,

How did you solve this? Here is my SQL statement:

SELECT * FROM Media JOIN Media_Genres on Media.media_id = Media_Genres.media_id JOIN Genres on Media_Genres.genre_id = Genres.genre_id WHERE Media_Genres.media_id = 1;

I am getting the error: Bummer! Nothing needs to be removed, you need to add a JOIN for the Genres ON the genre_id column in both the Media and Genres tables.

Kazi Ahmed
Kazi Ahmed
11,367 Points

SELECT * FROM Media JOIN Media_Genres ON Media_Genres.media_id = Media.media_id JOIN Genres ON Genres.genre_id = Media_Genres.genre_id WHERE Media_Genres.media_id=3;

anil rahman
anil rahman
7,786 Points

Maybe this challenge is a two part challenge so when you are adding the where clause in, its erroring because that's the next step?