WHERE Vs HAVING CLAUSE IN SQL

While working with SQL, one of the question which often creates confusion is whether to use where or having clause.Though it appears that both clauses do the same thing, they do it in different ways. Here in this article, I will try to highlight all the major differences between WHERE and HAVING, and things you should be aware of, when using either WHERE or HAVING.

WHERE Vs HAVING CLAUSE :

Though the HAVING clause specifies a condition that is similar to the purpose of a WHERE clause, the two clauses are not interchangeable. Listed below are some differences to help distinguish between the two:
1. The WHERE clause specifies the criteria which individual records must meet to be selected by a query. It can be used without the GROUP BY clause. The HAVING clause cannot be used without the GROUP BY clause.
2. The WHERE clause selects rows before grouping. The HAVING clause selects rows after grouping.
3. The WHERE clause cannot contain aggregate functions. The HAVING clause can contain aggregate functions.

WHERE vs HAVING Clause Example in SQL :

Now we will try to see the difference between where vs having clause through examples.In this example of WHERE and HAVING clause, we have two tables Employee and Department. Employee contains details of employees e.g. id, name, age, salary and department id, while Department contains id and department name. In order to show, which employee works for which department we need to join two tablesĀ on DEPT_ID to get the the department name. Our requirement is to find how many employees are working in each department and average salary of department. In order to use WHERE clause, we will only include employees who are earningĀ  more than 5000. Before executing our query which contains WHERE, HAVING, and GROUP BY clause, let see data from Employee and Department table:
SELECT * FROM Employee;

EMP_ID EMP_NAME EMP_AGE EMP_SALARY DEPT_ID
1 Virat 23 10000 1
2 Rohit 24 7000 2
3 Suresh 25 8000 3
4 Shikhar 27 6000 1
5 Vijay 28 5000 2

SELECT * FROM Department;

DEPT_ID DEPT_NAME
1 Accounting
2 Marketing
3 Sales

SELECT d.DEPT_NAME, count(e.EMP_NAME) as NUM_EMPLOYEE, avg(e.EMP_SALARY) as AVG_SALARY FROM Employee e,
Department d WHERE e.DEPT_ID=d.DEPT_ID AND EMP_SALARY > 5000 GROUP BY d.DEPT_NAME;

DEPT_NAME NUM_EMPLOYEE AVG_SALARY
Accounting 1 8000
Marketing 1 7000
Sales 2 8000

From the number of employee (NUM_EMPLOYEE) column you can see that only Vijay who work for Marketing department is not included in result set because his earning 5000. This example shows that, condition in WHERE clause is used to filter rows before you aggregate them and then HAVING clause comes in picture for final filtering, which is clear from following query, now Marketing department is excluded because it doesn’t pass condition in HAVING clause i..e AVG_SALARY > 7000
SELECT d.DEPT_NAME, count(e.EMP_NAME) as NUM_EMPLOYEE, avg(e.EMP_SALARY) as AVG_SALARY FROM Employee e,
Department d WHERE e.DEPT_ID=d.DEPT_ID AND EMP_SALARY > 5000 GROUP BY d.DEPT_NAME HAVING AVG_SALARY > 7000;

DEPT_NAME NUM_EMPLOYEE AVG_SALARY
Accounting 1 8000
Sales 2 8000

Add a Comment

Your email address will not be published. Required fields are marked *