Difference Between Where and Having Clause in SQL | Where vs Having Clause

By Mohit Uniyal|Updated : October 13th, 2022

Difference Between Where and Having Clause in SQL 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. The two Clauses Where and Having are the Clause in SQL queries that are used to filter records.

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

Difference 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:

Key Differences Between Where and Having Clause in SQL

Where Clause in SQL

Having Clause in SQL

On the specified condition, the WHERE Clause filters the records from the given table based.

On the specified condition, the HAVING Clause filters 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.

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 will satisfy the specified condition in the WHERE clause. Where Clause in SQL is also carried out by some operations such as SELECT, UPDATE, and DELETE statements.

Example of Where Clause in SQL

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

Difference Between Where and Having Clause in SQL PDF

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 that match the given condition will appear in the final result. The HAVING Clause is only used with the SELECT statement.

Example of Having Clause in SQL

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

 

Important GATE Topics
Difference between IoT and M2MDifference Between Array and List in Python
Difference Between Calloc() and Malloc()Difference Between Actual and Formal Parameters
Difference Between Procedural and Object-Oriented ProgrammingDifference Between Abstraction and Encapsulation in Java

Comments

write a comment

FAQs on Difference Between Where and Having Clause in SQL

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

  • The Difference Between Where and Having Clause in SQL as per the containment of aggregate function is that Where clause do not contain the aggregate function wheres the having function contains it.

  • The difference between Where and Having Clause in SQL is that Where clause is used ahead of the Group By cluase and the Having clause is used after it.

Follow us for latest updates