Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Other SQL Server Topics (2005)
 why this query like this???

Author  Topic 

kavi12345
Starting Member

19 Posts

Posted - 2012-03-14 : 07:04:17
Why the following queries are like this?

1st format:
SELECT Employee, SUM (Hours)
FROM EmployeeHours
WHERE SUM (Hours) > 10
GROUP BY Employee

2nd Format
SELECT Employee, SUM (Hours)
FROM EmployeeHours
GROUP BY Employee
HAVING SUM (Hours) > 10

This among formats,why the 2nd format only is accepetable? perhaps while using GROUP BY,we shouldnot use WHERE clause or what?? otherwise, Should we use HAVING clause while using GROUP BY only??..



kavichakravarthi.R

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2012-03-14 : 10:31:22
WHERE is to filter before grouping, HAVING is to filter after grouping.

An easy way to think of it is HAVING is a where clause for a GROUP BY.
Go to Top of Page

kavi12345
Starting Member

19 Posts

Posted - 2012-03-15 : 03:02:43
@russel...thanks dear..

kavichakravarthi.R
Go to Top of Page
   

- Advertisement -