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 trialKristian Woods
23,414 PointsSQL relationships between tables?
could somebody please explain what this SQL query means?
SELECT * FROM loans, books
WHERE loans.book_id = books.id;
thanks in advance
1 Answer
Steven Parker
231,184 PointsThis is an implicit INNER JOIN of two tables.
Let's break it down:
-
SELECT *
Show ALL columns... -
FROM loans, books
...from both the loans table and the books table... -
WHERE loans.book_id = books.id;
...where you can match book_id in loans with id in books
Note that any row of either table that cannot be matched with the other table will not be shown.
Another way to write the exact same query using an explicit join would be:
SELECT * FROM loans INNER JOIN books ON loans.book_id = books.id;