Differences between Having and Where in SQL and notes: sqlhaving
Differences:
The where clause removes rows that do not meet the where condition before grouping query results. That is, data is filtered before grouping. The where condition cannot contain a clustering function, use the where condition to filter out specific rows.
The having clause is used to filter groups that meet the conditions. That is, data is filtered after the group. The conditions often contain clustering functions and the having condition is used to filter out specific groups, you can also use multiple grouping standards for grouping.
Note:
1. The HAVING clause must be located before order by after group.
2. The WHERE statement is before the group by statement, and the SQL statement is calculated before the GROUP.
3. The HAVING statement is after the group by statement, and the SQL statement calculates the HAVING statement after the GROUP.
Example:
Select class, sum (Quantity) as quantity sum from A group by class having sum (Quantity)> 18
Example: joint use of Having and Where
Select category, SUM (Quantity) from A where quantity> 8 group by category having SUM (Quantity)> 10
Summary
The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.