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 SQL Basics Finding the Data You Want Review & Practice with SQL Playgrounds

SQL 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
Steven Parker
231,248 Points

This is an implicit INNER JOIN of two tables.

Let's break it down:

  • SELECT * :point_left: Show ALL columns...
  • FROM loans, books :point_left: ...from both the loans table and the books table...
  • WHERE loans.book_id = books.id; :point_left: ...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;