The HAVING clause in MySQL is used in combination with the GROUP BY clause to filter the results of aggregate functions applied to grouped rows. It allows you to specify conditions on the grouped data.
Here's the basic syntax:
SELECT column1, aggregate_function(column2)
FROM table_name
WHERE condition
GROUP BY column1
HAVING condition2;
For example, suppose you have a table named sales with columns product and amount, and you want to find the total sales for each product, but only for products with total sales greater than $1000:
SELECT product, SUM(amount) AS total_sales
FROM sales
GROUP BY product
HAVING total_sales > 1000;
This will give you a result where each row represents a unique product along with its total sales, but only for products where the total sales exceed $1000.