Tuesday, November 1, 2022

What is difference between Having Clause and Where Clause in SQL?

The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions.

 

Example:

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;


https://www.javatpoint.com/where-vs-having


Comparison BasisWHERE ClauseHAVING Clause
DefinitionIt is used to perform filtration on individual rows.It is used
to perform
 filtration
on groups.
BasicIt is implemented in row operations.It is implemented in
column operations.
Data fetchingThe WHERE clause fetches the specific data from particular rows based on the specified conditionThe HAVING clause
first fetches
the complete data.
It then separates
them according to the
given condition.
Aggregate FunctionsThe WHERE clause does not allow to work with aggregate functions.The HAVING clause can
work with
aggregate functions.
Act asThe WHERE clause acts as a pre-filter.The HAVING clause
acts as a post-filter.
Used withWe can use the WHERE clause with the SELECT, UPDATE, and DELETE statements.The HAVING clause can
only use with the
SELECT statement.
GROUP BYThe GROUP BY clause comes after the WHERE clause.The GROUP BY
clause
comes before
the
HAVING clause.

No comments:

Post a Comment

How to use SQL pagination using LIMIT and OFFSET

  How to extract just a portion of a larger result set from a SQL SELECT. LIMIT n is an alternative syntax to the FETCH FIRST n ROWS ONLY. T...