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

Julie Dowler
Julie Dowler
7,851 Points

SELECT after JOIN?

Link to the challenge: link

Here is the challenge. It's from Integrating PHP with Databases/Filtering Input for Queries/Multiple Conditionals:

Challenge Task 2 of 3

Note: We will be writing ONLY the SQL query for this challenge.

Along with the People table, we also have a Media table with media_id, title, img, format, year and category. To JOIN "many" media items with "many" people, we use a Media_People table which contains a media_id to link to the Media table and a people_id to link to the People table.

Modify your SELECT to pull Media title for all items that are linked to People with the last name "Tolkien"

You can start with the following code from the last task:

SELECT * FROM People WHERE fullname LIKE '%Tolkien';

So, here is my query:

SELECT * FROM People WHERE fullname LIKE '%Tolkien' JOIN Media_People ON People.people_id = Media_People.people_id JOIN Media ON Media_People.media_id = Media.media_id SELECT title FROM Media

2 Answers

Tom Gooding
Tom Gooding
16,735 Points

Hi Julie,

You SELECT the column as you would usually before the JOIN. However you need to specify the table as well. Here for example you need to SELECT Media.title FROM ...

SELECT Media.title FROM People 
INNER JOIN Media_people ON People.people_id = Media_People.people_id 
INNER JOIN Media ON Media_People.media_id = Media.media_id 
WHERE fullname LIKE '%Tolkien';
Julie Dowler
Julie Dowler
7,851 Points

That worked. Thank you!