|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
What is Difference WHERE and HAVING in SQL SERVERHi,
What is the difference between WHERE & HAVING and Proper uses of HAVING ? HAVING lets you apply a filter on an aggregate.
SELECT COUNT(column) FROM Products WHERE Active=1 GROUP By type HAVING COUNT(*) > 1 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 ? > > 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 quoteTibor Karaszi, SQL Server MVP http://www.karaszi.com/sqlserver/default.asp http://www.solidqualitylearning.com/ Blog: http://solidqualitylearning.com/blogs/tibor/ "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 ? >> >> > > 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 -- |
|||||||||||||||||||||||