Difference Between Where and Having Clause | Where Clause VS Having Clause

By Mohit Uniyal|Updated : April 28th, 2022

Difference Between Where and Having Clause in SQL: The two Clauses Where and Having are the Clause in SQL queries that are used to filter records.  The main difference between these two clauses is that the WHERE clause is used to determine a condition for filtering records before any combination is made, whereas the HAVING clause is used to determine a condition for filtering the values from a group.

Here, we will first briefly describe the Where and Having Clause in SQL and then we will see the complete list and elaborate on the difference between the Where and Having Clause in SQL. We have explained the differences in several parameters below.

Table of Content

What is the Where Clause in SQL?

WHERE Clause is used to search the records from the given table or the table’s rows before they are grouped. Only those records will be taken which are going to satisfy the specified condition in the WHERE clause. Where Clause in SQL is also carried out by some operations such as SELECT, UPDATE, DELETE statements. 

Example: Consider a table given below for “T-SHIRTS”

T-SHIRTS_ID

T-SHIRTS_SIZE

T-SHIRTS_PRICE

#010

S

230

#011

M

324

#012

L

430

#013

XL

730

Take the “Query” for example

SELECT T-SHIRTS_ID, PRICE FROM T-SHIRTS WHERE PRICE > 350

Output for this query can be given as

T-SHIRTS_ID

T-SHIRTS_PRICE

#012

430

#013

730

What is Having Clause in SQL?

The HAVING clause is generally used to filter the records of given data from the groups based on the given condition. This HAVING clause is used in the column functions and according to given conditions, it is applied to aggregate rows or groups. Those groups who will match the given condition will appear in the final result. The HAVING Clause is only used with the SELECT statement. 

Example: Take the table below for T-SHIRTS

T-SHIRTS_ID

SIZE

PRICE

#089

M

2200

#090

M

2000

#091

L

2500

Suppose, we want the count of T-SHIRTS with the count of size > L. Applying the “Having” clause to the content in the table.

SELECT COUNT(T-SHIRTS_ID), T-SHIRTS SIZE

FROM T-SHIRTS

GROUP BY SIZE

HAVING COUNT(SIZE) > L;

The output is mentioned below in the table:

COUNT(T-SHIRTS)

T-SHIRTS SIZE

2

M

What are the Differences Between Where and Having Clause in SQL?

As we have seen a fleeting introduction to Where and Having Clause in SQL. Now we will study the difference between Where and Having Clause in SQL. The notable differences between these two SQL are described in the table provided below:

Difference Between Where and Having Clause in SQL

Where Clause in SQL

Having Clause in SQL

On the specified condition, the WHERE Clause is used to filter the records from the given table based.

On the specified condition, the HAVING Clause is used to filter the record from the given groups based.

The WHERE clause can be used without considering the GROUP BY Clause.

The HAVING Clause cannot be used without considering the GROUP BY Clause

This Clause is implemented in row operations.

This Clause is implemented in column operation.

It cannot contain aggregate function.

It can contain aggregate functions.

SELECT, UPDATE, DELETE statement can be used with the WHERE Clause.

Only the HAVING Clause can be used with HAVING SQL.

This Clause is used before the GROUP BY Clause.

This Clause is used after the GROUP BY Clause.

With the single-row function like UPPER, LOWER etc WHERE Clause is used.

With multiple-row functions like SUM, COUNT etc HAVING Clause is used.

Comments

write a comment

FAQs

  • The basic difference between the Where and Having Clause in SQL is that the WHERE clause is used to indicate a condition for the filtration of records before making any group, whereas the HAVING clause is used to indicate a condition for the filtration of values from a group.

  • The HAVING clause is generally used for the filtration of groups based on records from the given data on the given condition. It is used in the column functions only and according to specific conditions, the HAVING clause is applied to aggregate rows or groups.

  • In SQL, the WHERE Clause is generally used to search the records from the given rows. Only those records will be chosen which satisfy the specified condition in this clause. Also, the WHERE Clause in SQL is carried out by some operations like SELECT, UPDATE, and DELETE statements.

  • The HAVING clause is used rather than the WHERE clause with aggregate functions. Whereas the GROUP BY Clause combines the rows that have the same values in the summary rows. The HAVING clause is utilised with the WHERE clause to search the rows with certain conditions. The HAVING clause is used after the GROUP BY clause.

  • Yes, the WHERE and HAVING clauses can be used together in a SELECT query. In the SELECT query, the WHERE clause is firstly used to filter individual rows and then the rows are grouped, perform aggregate calculations, and finally, the HAVING clause is used for the filtration of the groups.

Follow us for latest updates