Suppose I have two tables that are linked (one has a foreign key to the other) :
CREATE TABLE Document (
Id INT PRIMARY KEY,
Name VARCHAR 255
)
CREATE TABLE DocumentStats (
Id INT PRIMARY KEY,
DocumentId INT, -- this is a foreign key to table Document
NbViews INT
)
I know, this is not the smartest way of doing things, but this is the best example I could come up with.
Now, I want to get all documents that have more than 500 views. The two solutions that come to my mind are :
SELECT *
FROM Document, DocumentStats
WHERE DocumentStats.Id = Document.Id
AND DocumentStats.NbViews > 500
or :
SELECT *
FROM Document
INNER JOIN DocumentStats
ON Document.Id = DocumentStats.Id
WHERE DocumentStats.NbViews > 500
Are both queries equivalent, or is there one way that is far better than the other ? If so, why ?
I'm aware that my example is not perfect, and that the queries may need some tune-up, but I hope you got the point ;) !
EDIT: as requested in the answers, this question was aimed at MSSQL, but I would be interested in knowing if it is different for other DB engines (MySQL, etc...)