Updated
Introduction to MySQL HAVING clause
The HAVING is use along with the GROUP BY clause to filter the group based on a specified condition.
SELECT
column1,
column2,
column3,
aggregate_function(column4)
FROM
table_name
WHERE
condition_statement
GROUP BY
column1, column2, column3
HAVING
group_conditions;- Note that the
HAVINGclause only becomes operational after the theGROUP BYreturns a value, it is the value return by the GROUP BY that the HAVING filter according to the instruction. TheHAVINGclause applies the condition to groups of rows, while theWHEREclause applies the condition to individual rows. - Note that if you omit the GROUP BY clause in a syntax, the HAVING clause will behaves like a WHERE clause.
MySQL evaluates the HAVING clause after the FROM, WHERE, SELECT and GROUP BYclauses, but before DISTINCT, SELECT, ORDER BY, and LIMIT clauses:
Note that the SQL standard specifies that the HAVING is evaluated before SELECT clause and after GROUP BY clause.
MySQL HAVING clause examples
EXAMPLE 1:
EXAMPLE 2:
MySQL HAVING COUNT
In MySQL, when you need to group rows and then filter those groups based on an aggregate calculation (like counting the number of rows in each group), you use the combination of the GROUP BY clause with an aggregate function (such as COUNT()) and then filter the resulting groups with the HAVING clause. The key difference between WHERE and HAVING is that WHERE filters rows before grouping, while HAVING filters groups after aggregation.
How It Works
Grouping Data
GROUP BY: This clause arranges your rows into groups based on one or more columns. For example, if you have a table of sales, you can group the data by product name so that each group contains all sales for a specific product.
Aggregating Data
COUNT()Function: This function tallies the number of rows within each group. When combined withGROUP BY, it lets you see how many entries exist in each group.
Filtering Groups
HAVINGClause: After the data is grouped and the aggregate functions (likeCOUNT()) are calculated, theHAVINGclause is used to filter out groups that do not meet a certain condition. For example, you might want to display only those groups where the count is greater than a certain threshold.
Important Note:
In MySQL, you cannot use an alias defined in the SELECT clause (e.g., an alias for COUNT(c2)) within the HAVING clause because the HAVING clause is executed before the SELECT clause is processed. This means that the alias isn’t recognized when MySQL evaluates the HAVING condition.
Example 1: A Simple HAVING COUNT Query
Example 2: Practical HAVING with COUNT
Key Takeaways
- GROUP BY and Aggregation: The
GROUP BYclause organizes rows into groups, and functions likeCOUNT()calculate aggregate values for each group. - HAVING vs. WHERE: The
HAVINGclause filters groups after aggregation, unlike theWHEREclause which filters individual rows before grouping. - Alias Limitation: You cannot use an alias from the
SELECTclause in theHAVINGclause because of the order in which MySQL processes the query.
This detailed explanation and rewritten tutorial should help you understand how to use the HAVING clause along with the COUNT function to filter groups in MySQL.