Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

General Discussion

Database Foundations Joining Relational Data Between Tables in SQL Joining Tables and Aliasing Challenge

We have a 'movies' table with a 'title' and 'genre_id' column and a 'genres' table has an 'id' and 'name' column. Use an INNER JOIN to join the 'movies' and 'genres' tables together only selecting the movie 'title' first and the genre 'name' second.

I wrote:

SELECT movies.title, genres.name FROM movies INNER JOIN genres ON movies.title = genres.name;

Incorrect: Bummer! You're not retrieving the movie 'title' first and the genre 'name' second. Use an INNER JOIN.

What am I doing wrong?

5 Answers

hlias kantzos
hlias kantzos
19,191 Points

you use wrong key to match the two tables

i don't understand.. which key is wrong here? i did the same thing I can't figure out which key is wrong?

ummm. hello?? you never answered me

Will Lam
Will Lam
7,027 Points

I'm bumping into the same roadblocks as Wendell here.. would appreciate if anyone could help shed some light.. thanks!

Devin Gray
Devin Gray
39,261 Points

Basically you have to look at the question CAREFULLY, You have the first part right, joining the movies.title first and the genres.name second,

SELECT movies.title, genres.name FROM movies INNER JOIN

The next part is tricky, you're naming the wrong keys. Notice in the question that there are two different columns for each table, you have to replace the ON movies.title = genres.name with the other columns listed that you're not trying to list first and second. I'm horrible at explaining and it was a struggle for me as well but I hope that helps.

Will Lam
Will Lam
7,027 Points

Thanks Devin!~

Hunter MacDermut
Hunter MacDermut
15,865 Points

Correct answer:

SELECT movies.title, genres.name FROM movies JOIN genres ON movies.genre_id = genres.id;

I had to read the entry on JOIN syntax from the W3C to wrap my head around this: http://www.w3schools.com/sql/sql_join.asp

First, select the "title" column from the movies table & the "name" column from the genres table:

SELECT movies.title, genres.name

Then, specify which two tables you're joining information from:

FROM movies JOIN genres

Lastly, and this one is tricky, you must select the columns from the tables that refer to one another:

ON movies.genre_id = genres.id;

Which means, "the genre_id column from the movies table refers to the id column in the genres table". That is, the number assigned to each movie as its "genre_id" corresponds to the same number in the genres table which is just called "id". The ON keyword lets you join the tables by specifying which columns from each table refer to one another.

Does that help? I find that even though I mostly understand it that it's still difficult to describe because, like most syntax, it's convoluted.

Devin Gray
Devin Gray
39,261 Points

Much better explanation than I could give, I knew that was the answer but I wasn't sure why. That explanation makes much more sense. I know it helped me.

Thanks Hunter. http://www.w3schools.com/sql/sql_join.asp Pretty much help solving this puzzle. Its just third part which is tricky.