Hey.
jagadeesh kumar: I didn't really understand the difference, some are saying 1st case is not a proper join. is that true?
Not it's not true. It is a proper join, just a very "wide" join.
When you specify a list of table names, e.g:
FROM tbl1, tbl2, tbl3
, it is the equivelant of using a JOIN without an ON clause. - The result is the
Cartesian product of the tables (that is: each row in all tables is joined with every row in all the other tables), which in most cases will result in a rather huge result set. The WHERE clause is then applied to that massive result set to get you your results.
Specifying the ON clause will cause MySQL to choose the rows for the joined set more carefully, resulting in a smaller result. The filters are applied
before the joined set is compiled, which means the massive result set is never created.
I do not know exact details about the speed gains -- MySQL may well employ methods to reduce the loss of the Cartesian result set -- but it is generally advisable to use an ON clause.
Just to clarify, this query:
-
SELECT * FROM tbl1, tbl2
-
WHERE tbl1.id = tbl2.id
-
is exactly equivalent to this query:
-
SELECT * FROM tbl1 JOIN tbl2
-
WHERE tbl1.id = tbl2.id
-
And both of those should rather be written like this:
-
SELECT * FROM tbl1
-
JOIN tbl2 ON tbl1.id = tbl2.id
-
Also note that
JOIN
,
INNER JOIN
and
CROSS JOIN
are all exactly the same thing. The reason why they all exists, yet are all the same, is to comply with the standards and to provide compatibility with other SQL systems.