The difference and usage between where and having

Aggregate function is the key to compare where and having. 
Straight to the point. The execution order of where, aggregate functions, and having  after from:

where>aggregation function(sum,min,max,avg,count)>having

List group by to compare the two. () Because where and having are the most frequently asked when using group by) 
If you need to introduce an aggregate function to filter the results of group by, you can only use having. (Not much to say here, I want to execute the aggregate function first or filter first and then compare the execution order I listed above to see at a glance)

样例:select sum(score) from student  where sex='man' group by name having sum(score)>210
  • 1

Matters needing attention: 
1. Where cannot be followed by aggregate functions, because the execution order of where is greater than that of aggregate functions. 
2. The function of the where clause is to remove the rows that do not meet the where condition before grouping the query results, that is, to filter the data before grouping
3. The function of the having clause is to filter the groups that meet the conditions, that is , to filter the data after grouping . The conditions often include a grouping function. Use the having condition to display a specific group, or you can use multiple grouping criteria for grouping.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325904313&siteId=291194637