(ja************@yahoo.com) writes:
Hi Faculties,
I have two queries which give me the same output.
-- Query 1
SELECT prod.name, cat.name
FROM products prod INNER JOIN categories cat
ON prod.category_id = cat.id
WHERE cat.id = 1;
-- Query 2
SELECT prod.name, cat.name
FROM products prod INNER JOIN categories cat
ON prod.category_id = cat.id AND cat.id = 1;
The first query uses the WHERE clause and the second one has all the
conditions in the ON clause. Is there anthing wrong with the second
approach in terms of performance? Please suggest.
As long it's an inner join, it's only a matter of taste. I prefer to
put conditions on keys in the ON clause, and other conditions in the
WHERE clause. That is, if the condition is part of the join at all,
which it is not in this case.
If you have an outer join it's a completely different story, as you
could get different results. Common error:
SELECT ...
FROM a
LEFT JOIN b ON a.col1 = b.col1
WHERE b.type = 'X'
This query is effectively an inner join, because the WHERE clause
filters all rows where b.type are NULL. So here you need to put
b.type in the ON clause.
--
Erland Sommarskog, SQL Server MVP,
es****@sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx