Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Difference between Where and Having Clause in SQL

Where VS Having Clause in SQL: Explore the Difference between Where and Having Clause in SQL

The data found within the SQL series are filtered with the help of two clauses, “Where” and “Having”. Though similar, the differences in these terms arise when it is run and the GROUP BY Clause of the Select command. In this article, we have tried to explain the definition and the difference between where and having clause in SQL.

Ultimate Guide to Kickstart your GATE Exam Preparation
Download the e-book now

Clauses used to filter the records in the SQL series are divided into:

  • Where clause
  • Having clause

What is the “Where Clause”?

Where Clause is used to fetch/filter the records into rows before they are grouped. These data should specify or meet the mentioned condition. It will select and filter only the required data. Meanwhile, SQL will also implement ‘and’, ‘or’ and ‘not’ in the Where Clause, similar to the boolean condition. Operations such as “select, update and delete” are also carried out by this clause.

Example: Consider a table given below for “BOOKS”

BOOK_ID BOOK_NAME PRICE
89 A 230
90 B 430
91 C 324
92 D 730

Take the “Query” for example

SELECT BOOK_ID, PRICE FROM BOOKS WHERE PRICE > 350

Output for this query can be given as

BOOK_ID PRICE
B 430
D 730

What is the “Having Clause”?

Data from a group is fetched with the help of the “Having” clause. It tries to cater to the condition expected in the final result. The clause is applicable only with Select statements. The clause is used in column functions.

Example: Take the table below for BOOKS

BOOK_ID BOOK_NAME LANGUAGE PRICE
89 A Hindi 22000
90 B Hindi 20000
91 C English 25000

Consider, we want the count of books with the count of language > 1. Apply the “Having” clause to the content in the table.

SELECT COUNT(BOOK_ID), LANGUAGE

FROM BOOKS

GROUP BY LANGUAGE

HAVING COUNT(LANGUAGE) > 1;

The output to this is mentioned below:

COUNT(BOOK_ID) LANGUAGE
2 Hindi

What is the Difference between Where and Having Clause in SQL?

If “Where” clause is used to filter the records from a table that is based on a specified condition, then the “Having” clause is used to filter the record from the groups based on the specified condition. Learn more about what is the difference between Where and Having Clause in SQL from the table below.

Difference between Where and Having Clause in SQL

Where Clause in SQL Having Clause in SQL
Filter table based data catering to specific condition Group based data under set condition
Applicable without GROUP BY clause Does not function without GROUP BY clause
Row functions Column functions
Select, update and delete statements Only select statement
Applied before GROUP BY clause Used after GROUP BY clause
Used with single row operations such as Upper, Lower and so on Applicable with multiple row functions such as Sum, count and so on

Keep learning and stay tuned to BYJU’S to get the latest updates on GATE Exam along with GATE Eligibility CriteriaGATE 2024GATE Admit CardGATE Application FormGATE SyllabusGATE CutoffGATE Previous Year Question Paper, and more.

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*