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? Still need help, please!

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```
Julie Dowler
Julie Dowler
7,851 Points

Thank you! I was able to understand it after reading that.

1 Answer

Edward Mendez
Edward Mendez
5,562 Points

This is a SQL syntax issue.

Basic SQL Syntax Queries should order itself

SELECT (column names) FROM (table) (JOIN table ON id/ref) WHERE condition;

Update your query to:

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

This will specifically pull the title field from the Media table where the fullname is LIKE Tolkien.