:::: MENU ::::

Using HAVING clause in T-SQL scripts

  • Jan 29 / 2009
  • 0
dbDigger, Having Clause, T-SQL Tips and Tricks

Using HAVING clause in T-SQL scripts

Having clause i used to filter the results produced by GROUP BY clause. WHERE clause filters the result before selection while HAVING clause is used to filter the results produced by GROUP BY clause. That is the reason that WHERE clause is always used before GROUP BY clause and HAVING clause is used after GROUP BY clause. Here is an example to use HAVING clause for filtration of GROUP BY results.

 
USE AdventureWorks  
 GO  
 SELECT OrderDate,SUM(TotalDue) TotalDueByOrderDate  
 FROM Sales.SalesOrderHeader  
 WHERE OrderDate BETWEEN '7/1/2001' AND '7/31/2001'  
 GROUP BY OrderDate  
 Having Sum(TotalDue) <>  

ResultOfHaving
In result you may notice that HAVING clause filtered on result produced through GROUP BY clause and results qualified the condition in HAVING clause.

Consult us to explore the Databases. Contact us