Difference between a HAVING CLAUSE and a WHERE CLAUSE



Only with the SELECT statement, we can use HAVING Clause.  Basically HAVING Clause is used with GROUP BY Clause. Without the use of the GROUP BY clause, HAVING behaves like a WHERE clause.



WHERE CLAUSE
HAVING CLAUSE
1-   WHERE clause applies to individual rows.

2-  The WHERE clause is applied first to the individual rows in the tables, which means the WHERE clause is fetched data from memory according to condition.

3-  We can use the WHERE clause with the HAVING clause, but it applies before the GROUP BY clause.     
1-   HAVING clause is like a WHERE clause, but applies only to groups as a whole.
2-  The HAVING clause is applied to the rows in the result set, which means the first data is fetched from memory to result set then apply the HAVING clause on the row of the result set.

3-  We can apply a HAVING clause only to columns that also appear in the GROUP BY clause or in an aggregate function



Where Clause Example:

SELECT empName,empDept,empSalary WHERE empSalary=20000



HAVING Clause Example:

SELECT empName,AVG(empSalary)

GROUP BY empName,empSalary HAVING AVG(empSalary)>20000




No comments:

Post a Comment

Please do not enter any spam link in the comment box.

Related Posts

What is the Use of isNaN Function in JavaScript? A Comprehensive Explanation for Effective Input Validation

In the world of JavaScript, input validation is a critical aspect of ensuring that user-provided data is processed correctly. One indispensa...