Monday 3 August 2015

What are the difference between Having and Where Clause?

Here are some useful difference between WHERE and HAVING clause in SQL :

1) WHERE clause can be used with SELECT, UPDATE and DELETE statements and  clauses but HAVING clause can only be used with SELECT statements.

e.g.

SELECT * FROM Employee WHERE EmployeeId=3

will print details of employee with id = 3.


Similarly,

SELECT EmployeeName, COUNT(EmployeeName) AS NumberOfEmployee FROM Employee HAVING COUNT(EmployeeName) > 2;

will print duplicate employees from table. 


2) We can't use aggregate functions in the where clause unless it is in a sub query contained in a HAVING clause whereas  we can use aggregate function in Having clause. We can use column name in Having clause but the column must be contained in the group by clause.

3) WHERE clause is used on the individual records whereas Having Clause in conjunction with Group By Clause work on the record sets ( group of records ).

No comments:

Post a Comment