This article is an excerpt from the MySQL must know
I. Filtering grouping
In addition to using group by grouping data, MySQL also allows filtering of groupings, including which groupings to exclude and which groups are excluded. For example, you might want to list all customers with at least two orders. To arrive at this data, filtering must be based on a complete grouping rather than individual rows
We have seen the role of the WHERE clause, but in this example where the task cannot be done because the where filter specifies rows rather than groupings. In fact, where there is no concept of grouping
So, what does not use where to use? MySQL provides an additional clause for this purpose, which is the HAVING clause. Having is very similar to where. In fact, all the types of where clauses that have been learned so far can be replaced with having. The only difference is where filter rows, and having filter groups
Two. Example
the last line of this SELECT statement adds a HAVING clause that filters those groupings of Count (*) >=2 (more than two orders)
three. The difference between having and where
Here is another way of understanding where the data is filtered before grouping, having the data grouped and filtered. This is an important difference.
Filtering grouping in MySQL