Home All Groups Group Topic Archive Search About

What is Difference WHERE and HAVING in SQL SERVER

Author
19 Aug 2005 12:37 PM
DMP
Hi,
What is the difference between WHERE & HAVING and Proper uses of HAVING ?

Author
19 Aug 2005 12:50 PM
Roji. P. Thomas
HAVING lets you apply a filter on an aggregate.

SELECT COUNT(column)
FROM Products
WHERE Active=1
GROUP By type
HAVING COUNT(*) > 1


--
Roji. P. Thomas
Net Asset Management
http://toponewithties.blogspot.com


Show quote
"DMP" <debdulal.mahapa***@fi-tek.co.in> wrote in message
news:eNeM$qLpFHA.1996@TK2MSFTNGP10.phx.gbl...
> Hi,
> What is the difference between WHERE & HAVING and Proper uses of HAVING ?
>
>
Author
19 Aug 2005 1:06 PM
Tibor Karaszi
Phrasing it differently:

The clauses are performed in below order:
WHERE
GROUP BY
HAVING

A later clause will operate on the output from the immediate earlier clause.

Show quote
"Roji. P. Thomas" <thomasr***@gmail.com> wrote in message
news:eou3mwLpFHA.2976@TK2MSFTNGP12.phx.gbl...
> HAVING lets you apply a filter on an aggregate.
>
> SELECT COUNT(column)
> FROM Products
> WHERE Active=1
> GROUP By type
> HAVING COUNT(*) > 1
>
>
> --
> Roji. P. Thomas
> Net Asset Management
> http://toponewithties.blogspot.com
>
>
> "DMP" <debdulal.mahapa***@fi-tek.co.in> wrote in message
> news:eNeM$qLpFHA.1996@TK2MSFTNGP10.phx.gbl...
>> Hi,
>> What is the difference between WHERE & HAVING and Proper uses of HAVING ?
>>
>>
>
>
Author
19 Aug 2005 12:54 PM
David Portas
HAVING is applied after the results are aggregated (grouped). WHERE is
applied before. If the HAVING clause references only the columns that
are included in the GROUP BY list then it is logically equivalent to
WHERE. Usually, however, HAVING refers to some expression including an
aggregate function - something you can't do in the WHERE clause.

Example (from the Northwind database):

/* Customers who have made more than 20 orders */

SELECT customerid
FROM Orders
GROUP BY customerid
HAVING COUNT(*)>20 ;

--
David Portas
SQL Server MVP
--

AddThis Social Bookmark Button