×

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

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 Aliases

SQL Aliases: These are used to give a temporary name for a column or table. These are used to make tables or columns more readable. These are used to rename the table...

2 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 minutes read.

How to use HAVING clause in SQL

In this article, we will learn about the HAVING clause concept and how to use it in SQL. What is the HAVING clause? In Structured Query Language, HAVING Clause used with GROUP...

7 minutes read.

TCL Commands in SQL

In Structured Query Language, TCL is an abbreviation for Transaction Control Language. A single unit of work in a database is formed after the consecutive execution of commands is known...

6 minutes read.

SQL NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

7 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.

SQL INSERT Statement

In this tutorial, we will help you to understand and learn how to insert records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

SQL Count

Structured Query Language Count() Function is used with Structured Query Language SELECT Statement. SQL Count() function returns the number of items that match the specified criteria in the SELECT statement. Count()...

2 minutes read.

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL? In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL....

4 minutes read.

SQL GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 minutes read.

SQL Handling Duplicate

Removing Duplicates using DISTINCT Keyword: By using the DISTINCT keyword in SQL we can remove duplicate characters from tables or databases. A table contains more duplicate values and duplicate values can cause...

4 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.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

4 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.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.

Codd’s Rules in SQL

Codd’s Rules Dr. Edgar F. Codd, in 1985, laid down 13 fundamental rules after doing large-scale research on the Relational Model of databases. According to him, every database must follow these...

3 minutes read.

SQL Right Join

The SQL Right Join query displays all the table records and similar records from the left table. The query display zero records if it doesn’t find any similar records. If...

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.