In MySQL, The "HAVING" clause is used in conjunction with the "GROUP BY" clause to filter the results of a grouped query based on a specified condition.
It is similar to the "WHERE" clause but is used specifically with grouped data.
The "HAVING" clause is particularly useful when we want to filter the result set based on aggregate values (like averages, counts, and sums) after using the "GROUP BY" clause.
Note: The "WHERE" clause filters rows before they are grouped, The "HAVING" clause filters groups after they have been formed.
SELECT column1, aggregate_function(column2), ... FROM table_name GROUP BY column1, ... HAVING condition;
column1: The column by which we want to group the results.
aggregate_function(column2): An aggregate function applied to another column within each group.
table_name: The name of the table from which we want to retrieve data.
HAVING: Specifies the condition to filter the groups.
SELECT category, AVG(price) AS avg_price FROM products GROUP BY category HAVING avg_price > 10000;
Note: This query groups the products by the category column, calculates the average price for each category, and then filters the results to include only those groups where the average price is greater than 10000.