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 Basis | WHERE Clause | HAVING Clause |
---|---|---|
Definition | It is used to perform filtration on individual rows. | It is used to perform filtration on groups. |
Basic | It is implemented in row operations. | It is implemented in column operations. |
Data fetching | The WHERE clause fetches the specific data from particular rows based on the specified condition | The HAVING clause first fetches the complete data. It then separates them according to the given condition. |
Aggregate Functions | The WHERE clause does not allow to work with aggregate functions. | The HAVING clause can work with aggregate functions. |
Act as | The WHERE clause acts as a pre-filter. | The HAVING clause acts as a post-filter. |
Used with | We can use the WHERE clause with the SELECT, UPDATE, and DELETE statements. | The HAVING clause can only use with the SELECT statement. |
GROUP BY | The GROUP BY clause comes after the WHERE clause. | The GROUP BY clause comes before the HAVING clause. |
No comments:
Post a Comment