×

WHERE Clause vs HAVING Clause

The WHERE Clause and HAVING clause filter the records in the Structured Query Language queries. The main difference between the WHERE clause and the HAVING clause is the WHERE clause is used to specify a condition for filtering records before any groupings are made. It cannot be used with an aggregate function, while the HAVING clause specifies conditions for filtering records from a group and its work with an aggregate function. Before comparing both clauses, we will understand each clause separately.

WHERE Clause

The WHERE clause in the Structured Query Language is used to specify the conditions for filtering the records. WHERE clause is used with the SELECT, INSERT, UPDATE and DELETE statement to filter records from the table.

The JOIN clause (WHERE and HAVING) mentions specific conditions to retrieve records from a single table or multiple tables. Not only to retrieve data but also to delete and update the records from the tables. It only retrieves the data, deletes, or updates the table if the mentioned condition is satisfied. The WHERE clause mention condition on the selected column or columns.

The Following Syntax of the WHERE Clause:

SELECT COLUMNS FROM TABLE_NAME WHERE CONDITIONS GROUP BY COLUMN;

The WHERE Clause in the Structured Query Language can also use the Logical Operators, such as AND, OR, and NOT. The Logical operator expression uses the comparison operator as their operands. It also used the Comparison operator like <, <=, >, >= and <>.  The Comparison operators are used to compare the values in the table, i.e., string values and arithmetic expressions.

Let's take an example of the WHERE clause. We have a table named Employees that contain the following records.

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERIDWORKING_HOURSGENDER
1001VAIBHAVIMISHRA65000PUNEORACLE112F
1002VAIBHAVSHARMA60000NOIDAORACLE19M
1003NIKHILVANI50000JAIPURFMW210M
2001PRACHISHARMA55500CHANDIGARHORACLE110F
2002BHAVESHJAIN65500PUNEFMW212M
2003RUCHIKAJAIN50000MUMBAITESTING49F
3001PRANOTISHENDE55500PUNEJAVA39F
3002ANUJAWHERE50500JAIPURFMW29F
3003DEEPAMJAUHARI58500MUMBAIJAVA312M
4001RAJESHGOUD60500MUMBAITESTING410M

We will write the following query if we want to get the employee id, first name, last name, employee city, salary, and gender information whose salary is greater than 55000.

SELECT EMPLOYEEID, FIRST_NAME, LAST_NAME, CITY, SALARY, GENDER FROM EMPLOYEES WHERE SALARY >55000; 

The following output shows employee id, first name, last name, salary, city, and gender whose employee salary is greater than 55000.

WHERE Clause vs HAVING Clause

The above query is just simply using the where clause. If we use the GROUP BY clause in the above query, the result will differ from the above result.

SELECT EMPLOYEE, FIRST_NAME, LAST_NAME, CITY, SALARY, GENDER FROM EMPLOYEES WHERE SALARY >55000 GROUP BY SALARY; 

It gives the following output:

WHERE Clause vs HAVING Clause

We use the Salary field with GROUP BY Clause, which means first records get a filter specified in the WHERE Clause Condition and after the result is grouped using salary fields as we all can see the difference in both the output.

We will use AND logical operator with WHERE Clause.

In this example, we want to get employee id, first name, last name, city, salary whose salary is greater than 45000 and less than equal to 60000.

SELECT EMPLOYEEID, FIRST_NAME, LAST_NAME, SALARY, CITY FROM EMPLOYEES WHERE SALARY >= 45000 AND SALARY <= 60000.

In the following output, we can see employee id, first name, last name, salary, and the city whose employee salary is greater than 45000 and less than equal to 60000.

WHERE Clause vs HAVING Clause

HAVING Clause

HAVING Clause in Structured Query Language used with GROUP BY Clause specifies conditions that filter which group results appear in the output. It returns only those data from the group, which fulfills the conditions. With the HAVING clause, we can use the WHERE clause also in the query. If we use Both Clauses together, WHERE clause will get executed first where it will filter the individual rows, then rows are grouped and at the end, HAVING Clause filters the groups.

HAVING clause conditions are placed after the GROUP BY clause. HAVING clause behaved same as WHERE clause in the Structured Query Language does not use the GROUP BY clause. We can use aggregate functions such as MIN, MAX, SUM, AVG, and COUNT. This function is only used with the SELECT clause and the HAVING clause.

The Following Syntax of the WHERE Clause:

SELECT COLUMNS, AGGREGATE FUNCTION, FROM TABLENAME WHERE CONDITION GROUP BY COLUMN HAVING CONDITIONS;  

Let's take an example of the HAVING clause. We have a table named Employees that contain the following records.

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERIDWORKING_HOURSGENDER
1001VAIBHAVIMISHRA65000PUNEORACLE112F
1002VAIBHAVSHARMA60000NOIDAORACLE19M
1003NIKHILVANI50000JAIPURFMW210M
2001PRACHISHARMA55500CHANDIGARHORACLE110F
2002BHAVESHJAIN65500PUNEFMW212M
2003RUCHIKAJAIN50000MUMBAITESTING49F
3001PRANOTISHENDE55500PUNEJAVA39F
3002ANUJAWHERE50500JAIPURFMW29F
3003DEEPAMJAUHARI58500MUMBAIJAVA312M
4001RAJESHGOUD60500MUMBAITESTING410M

Suppose we want to get the employee id, first name, last name, employee city, the average salary of employees, and gender information for employees whose employee average salary is greater than 50000 for this. In that case, we will write the following query.

SELECT EMPLOYEE, FIRST_NAME, LAST_NAME, CITY, AVG(SALARY) AS SALARY, GENDER FROM EMPLOYEES GROUP BY SALARY HAVING AVG(SALARY) >50000;

It gives the following output:

WHERE Clause vs HAVING Clause

Differences between Where Clause and HAVING Clause

Below are the following differences between the Where Clause and the HAVING clause, such as:

WHERE ClauseHAVING Clause
WHERE clause is a pre-filter clause.HAVING clause is a post-filter clause.  
WHERE clause is used with SELECT statement, UPDATE statement, DELETE statement.But without the SELECT statement, we cannot operate using the HAVING clause.
The WHERE clause fetches the records from the table on the given specified conditions.Whereas the HAVING clause retrieves entire records from the table, grouping is done based on the condition.  
WHERE clause is used in the query to filter the individual rows.On the other hand, the HAVING clause filters the records based on the groups, not individual rows.
We use the WHERE clause to filter individual rows, so we cannot use aggregate functions with the WHERE clause because the aggregate function is used for the entire columns.But we can use aggregate functions with the HAVING clause because the HAVING clause is used to filter the entire column (groups).
We can say the WHERE clause is faster than HAVING because the WHERE clause is placed before the GROUP BY clause, which means rows get filtered first, then the operation is formed on the aggregate operation or function.HAVING clause is placed after the GROUP BY clause, which means aggregate function calculation is performed, then the HAVING clause filters the data. Apart from this situation, we can say the WHERE clause is faster than the HAVING clause, so it's better to avoid the HAVING clause wherever possible.

Related Topics

How to Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

4 minutes read.

SQL Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

2 minutes read.

SQL Temporary Tables

Temporary Table: The Temporary tables are generally created in TempDB. If the last connection is terminated then the tables are deleted automatically. These are generally used to store and process the...

4 minutes read.

SQL IN vs SQL EXISTS

SQL IN vs SQL EXISTS This article discusses in detail about the IN and the EXISTS operators in SQL. It is a common question between developers that what is the difference...

3 minutes read.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

6 minutes read.

What is SQL Injection?

Introduction to SQL Injection SQL injection is a vulnerability or a technique that might destroy the database of a website or a web application. It is one of the most widely...

4 minutes read.

How to drop a column in SQL?

How to drop a column in SQL Introduction To delete a column from an already created table, one needs to use the ALTER command along with the DROP COLUMN clause. Syntax: ALTER TABLE tablename...

4 minutes read.

SQL SELECT OR Operator

This SQL tutorial explains and helps us understand how to use the OR operator in the SELECT query with examples. The OR operator in the Structured Query Language is used to...

3 minutes read.

SQL SELECT MAX

The SQL Max() function is an aggregate function in SQL. This function returns the values which are greater in the condition. The condition may be a number, or it may...

5 minutes read.

SQL SELECT BETWEEN Operator

This SQL tutorial explains and helps us understand how to use the BETWEEN operator in the SELECT query with examples. The SQL SELECT BETWEEN operator retrieves the data from the table...

2 minutes read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

Nth highest salary

The most common and important question asked in interviews that how we can find the Nth highest salary in a table (2nd highest salary, 3rd highest salary, or Nth highest...

6 minutes read.

How to use the BETWEEN operator in SQL

In this entire SQL article, we will understand and learn about the BETWEEN operator concept and how to use it in SQL. What is the BETWEEN operator in SQL? The Between operator...

4 minutes read.

How to Create Temporary Table in SQL?

How to Create Temporary Table in SQL  Introduction to Temporary Tables Temporary table is a table which is used to store temporary data that can be used further in the same client...

3 minutes read.

SQL Syntax

In this tutorial, we will help you understand about the different SQL Syntax concepts with the help of an example. Every Structured Query Language starts with Keywords like select, insert, update,...

5 minutes read.

SQL INTERSECT

SQL intersect operator is used to combine two or more SELECT statements, but it only displays the data similar to the SELECT statement. The syntax for the INTERSECT operation: SELECT COLUMN_NAME1, COLUMN_NAME2,...

3 minutes read.

SQL Queries

In a database, queries are used to request the result set of data from the table or action on the records. A Query can answer your simple or complicated question, perform...

5 minutes read.

SET Operators in SQL

The operator used to join or combine two queries is none other than SET operators. Operators categorized into SET operators are as follows: UNION Operator.UNION ALL’ Operator.INTERSECT Operator.MINUS Operator. Rules to be...

8 minutes read.

SQL Data Definition Language

Data definition language directly effects on the structure/schema of the database. CREATE, ALTER, DROP are the commands of DDL.CREATE: Creates new database, table, or view of table.ALTER: Modifies the database or...

3 minutes read.