dbTalk Databases Forums  

Adding conditions in the ON clause of a JOIN

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Adding conditions in the ON clause of a JOIN in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
jackal_on_work@yahoo.com
 
Posts: n/a

Default Adding conditions in the ON clause of a JOIN - 02-12-2008 , 01:10 AM






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.


Thanks in advance
Jackal

Reply With Quote
  #2  
Old   
Madhivanan
 
Posts: n/a

Default Re: Adding conditions in the ON clause of a JOIN - 02-12-2008 , 08:05 AM






On Feb 12, 12:10*pm, jackal_on_w... (AT) yahoo (DOT) com wrote:
Quote:
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.

Thanks in advance
Jackal

No issues if you use INNER JOINs
You may get different results if you use OUTER JOINs


Reply With Quote
  #3  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: Adding conditions in the ON clause of a JOIN - 02-12-2008 , 08:20 AM



Nothing wrong, it is more a question of preference. There is no logical
difference between ON and WHERE for inner joins, and has no effect on
performance. I normally prefer to write queries like the first example to
separate join conditions from filters.

There is only one situation where this may matter. SQL Server supports the
proprietary GROUP BY ALL. In that case the rows removed by the WHERE filter
are added back, so moving the filter between ON and WHERE will make a
difference.

Of course, this is valid for inner joins only, for outer joins it does
matter where you place filters and you will get different results.

HTH,

Plamen Ratchev
http://www.SQLStudio.com


Reply With Quote
  #4  
Old   
--CELKO--
 
Posts: n/a

Default Re: Adding conditions in the ON clause of a JOIN - 02-12-2008 , 12:42 PM



Correcting your data element names a bit (vague dangling "id" and
"name"; the absurd "category_id" that changes names from table to
table), you can also write:

SELECT P.product_name, C.foobar_category
FROM Products AS P, VagueCategories AS C
WHERE P.foobar_category = C.foobar_category
AND C. foobar_category = 1;

Reply With Quote
  #5  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Adding conditions in the ON clause of a JOIN - 02-12-2008 , 04:47 PM



(jackal_on_work (AT) yahoo (DOT) com) writes:
Quote:
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, esquel (AT) sommarskog (DOT) 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


Reply With Quote
  #6  
Old   
Tony Rogerson
 
Posts: n/a

Default Re: Adding conditions in the ON clause of a JOIN - 02-13-2008 , 11:28 PM



Quote:
SELECT P.product_name, C.foobar_category
FROM Products AS P, VagueCategories AS C
WHERE P.foobar_category = C.foobar_category
AND C. foobar_category = 1;
In SQL Server nobody uses that format any more in fact you'll confuse most
developers and leave yourself open to a cartesian product problem which was
common before we start using the ANSI 92 introduced INNER JOIN syntax.

Best practice definied in Books Online is to use INNER JOIN instead of the
prehistoric ANSI 89 style.

--
Tony Rogerson, SQL Server MVP
http://sqlblogcasts.com/blogs/tonyrogerson
[Ramblings from the field from a SQL consultant]
http://sqlserverfaq.com
[UK SQL User Community]



Reply With Quote
  #7  
Old   
Peter Deacon
 
Posts: n/a

Default Re: Adding conditions in the ON clause of a JOIN - 02-23-2008 , 06:06 AM



"Tony Rogerson" <tonyrogerson (AT) torver (DOT) net> wrote in
news:fp0jlr$m43$1$8300dec7 (AT) news (DOT) demon.co.uk:

Quote:
SELECT P.product_name, C.foobar_category
FROM Products AS P, VagueCategories AS C
WHERE P.foobar_category = C.foobar_category
AND C. foobar_category = 1;

In SQL Server nobody uses that format any more in fact you'll confuse
most developers and leave yourself open to a cartesian product problem
which was common before we start using the ANSI 92 introduced INNER
JOIN syntax.

Best practice definied in Books Online is to use INNER JOIN instead of
the prehistoric ANSI 89 style.

Personally I get burned *by far* more for forgetting the WHERE clause and
having the conditions mingle with the JOIN operators (rather than the
preferred syntax error) than we ever had cartesian product problems back in
the days when ANSI joins didn't exist.

Cart product production is typically obvious. Weird combinations that can
arise from committing the above mistake are almost always non-trivial and
non-obvious.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.