I have a users
table and a books
table. Two tables have a pretty standard One-to-Many relationship. Their data and structure are as follow:
To get the information of what books does each user have, I would normally do a standard JOIN
query:
SELECT * FROM users JOIN books ON users.id = books.user_id
At the same time, I came across with another query, that seems to do the same work, which writes as:
SELECT * FROM users, books WHERE users.id = books.id
These two queries seems to give the exactly same result as follow
The second query without the JOIN
clause I see in one of Standford's online courses and it seems to work fine. However I was taught to use, and saw other developers using the JOIN
as the approach to this type of relationship. My questions are:
- Are those two queries equivalent in terms of the query result?
- Is there a reason why we should use one over the other?
- Do the SQL engine perform different when executing these two ?