![]() |
VOOZH | about |
MySQL provides the HAVING clause to filter grouped data based on aggregate conditions. It is useful for applying conditions on results obtained after using the GROUP BY clause.
GROUP BY clause. SELECT column1, column2, AGGREGATE_FUNCTION(column_name)
FROM table_name
GROUP BY column1, column2
HAVING AGGREGATE_FUNCTION(column_name) condition;Where:
column1, column2: Columns to include in the result.table_name: The table from which to retrieve data.AGGREGATE_FUNCTION(column_name): An aggregate function applied to a column.condition: The condition to filter the aggregated results.To understand the HAVING clause, we will create a sample table and use it to demonstrate how grouping and filtering work together.
👁 Screenshot-2026-03-26-143820In this example we will the find products that have been sold more than once.
Query:
SELECT product, COUNT(*)
FROM sales
GROUP BY product
HAVING COUNT(*) > 1;Output:
👁 Screenshot-2026-03-26-151221Here we will find products with a total quantity sold greater than 10
Query:
SELECT product, SUM(quantity)
FROM sales
GROUP BY product
HAVING SUM(quantity) > 10;Output:
👁 Screenshot-2026-03-26-151339