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 trialErick Amezcua
21,826 PointsTraveling through the tables (joins problem)
It's posible with one query make a join to a "distant" table. There is my database diagram: https://imgur.com/a/IjBxv
Can I know in which region an employee works by looking for his name?. Starting in employees table and end in the regions table?
1 Answer
Steven Parker
231,269 PointsSure, you just have to dig through the relationships to get there:
SELECT E.first_name, E.last_name, R.region_name
FROM EMPLOYEES E
INNER JOIN DEPARTMENTS D ON E.department_id = D.department_id
INNER JOIN LOCATIONS L ON D.location_id = L.location_id
INNER JOIN COUNTRIES C ON L.country_id = C.country_id
INNER JOIN REGIONS R ON C.region_id = R.region_id
-- the next line is optional, without it you get the region for ALL employees
WHERE E.employee_id = --{ID of the one you want}
Now please tell me I didn't just do your homework.
Erick Amezcua
21,826 PointsErick Amezcua
21,826 PointsActually yes, but now it all makes sense, I did not know that a join could be made after a join. Thank you so much