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

Cant got the problem, Help this statement. (SQL)

QUERY: SELECT * FROM Media JOIN Media_Genres ON Media_Genres.media_id = Genres.genres_id WHERE Media.media_id=3;

Question: 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.

4 Answers

I've just tried this challenge again and it's a bit confusing. Finally....!

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;

Thanks..

Steven Parker
Steven Parker
231,140 Points

Here's a few hints:

  • when you join the Media_Genres table, you probably want to match just media_id columns
  • you will need a separate JOIN for the Genres table
  • when you join the Genres table, you will probably want to match genre_id columns

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

SQL Error: no such column: Genres.genres_id

Please explain and correct it. Also explain correct query. Step by step.

Oleksii Temchenko
Oleksii Temchenko
3,474 Points

I know, that I am quite late with answer, but this what I ended with.

SELECT * FROM Media JOIN Media_Genres ON  Media.media_id = Media_Genres.media_id

//Basically now we have genre_id column in Media, connected with media_id, which are equal in both Media/Media_Genres tables. Also Media and Media_Genres have common media_id and we are obliged to use Media_Genres as joint table.

JOIN Genres ON Genres.genre_id = Media_Genres.genre_id WHERE Media_Genres.media_id = 3

//Now Genres joins the party. We basically adding 1 needed genre (with id=3) to Media table by adding WHERE Clause to 'ON Genres.genre_id = Media_Genres.genre_id', as Media and Media_Genres have common media_id.

This is how we obey the condition that the media_id column should not be ambiguous.

Hope it helps