SQL HAVING Get Certified
Learn how to filter the groups produced by GROUP BY using the HAVING clause together with aggregate functions such as COUNT, SUM, AVG, MIN and MAX.
What is the HAVING Clause?
The HAVING clause filters groups after they have been created by GROUP BY and after aggregate calculations have been performed.
While WHERE removes individual rows before any grouping happens, HAVING removes entire groups based on the results of aggregate functions.
Basic HAVING Syntax
HAVING is almost always used together with GROUP BY and at least one aggregate function.
What this code does: Calculates total sales per category and keeps only those categories whose total is greater than 1000. Books (1200) and Games (2100) remain; Clothing is filtered out.
WITH sales(sale_id, category, amount) AS (
VALUES
(1, 'Books', 500),
(2, 'Books', 700),
(3, 'Games', 1200),
(4, 'Games', 900),
(5, 'Clothing', 400)
)
SELECT
category,
SUM(amount) AS total_sales
FROM sales
GROUP BY category
HAVING SUM(amount) > 1000
ORDER BY total_sales DESC;
HAVING with COUNT()
A very common requirement is to keep only groups that contain a minimum number of rows.
What this code does: Counts students per course and keeps only courses that have at least two enrolments. SQL (3) and Python (2) remain; HTML is dropped.
WITH enrollments(enrollment_id, course, student_name) AS (
VALUES
(1, 'SQL', 'Aarav'),
(2, 'SQL', 'Meera'),
(3, 'SQL', 'Kabir'),
(4, 'Python', 'Riya'),
(5, 'Python', 'Dev'),
(6, 'HTML', 'Tara')
)
SELECT
course,
COUNT(*) AS student_count
FROM enrollments
GROUP BY course
HAVING COUNT(*) >= 2
ORDER BY student_count DESC;
HAVING with AVG()
You can filter groups according to their average value.
What this code does: Calculates the average score of each department and keeps only those departments whose average is 80 or higher. Engineering (90) and Design (81) remain; Marketing is filtered out.
WITH employee_scores(employee_id, department, score) AS (
VALUES
(1, 'Design', 78),
(2, 'Design', 84),
(3, 'Engineering', 92),
(4, 'Engineering', 88),
(5, 'Marketing', 65),
(6, 'Marketing', 72)
)
SELECT
department,
ROUND(AVG(score), 2) AS average_score
FROM employee_scores
GROUP BY department
HAVING AVG(score) >= 80
ORDER BY average_score DESC;
HAVING with MIN()
MIN() lets you keep groups whose smallest value still satisfies a condition.
What this code does: Finds the lowest price in each category and keeps only categories whose cheapest item costs at least 15 000. Laptop (55 000) and Phone (18 000) remain; Tablet is dropped.
WITH product_prices(product_id, category, price) AS (
VALUES
(1, 'Laptop', 55000),
(2, 'Laptop', 62000),
(3, 'Phone', 18000),
(4, 'Phone', 24000),
(5, 'Tablet', 12000),
(6, 'Tablet', 15000)
)
SELECT
category,
MIN(price) AS lowest_price
FROM product_prices
GROUP BY category
HAVING MIN(price) >= 15000
ORDER BY lowest_price DESC;
HAVING with MAX()
Likewise, MAX() can be used to keep groups whose highest value meets a threshold.
What this code does: Finds the highest temperature recorded in each city and keeps only cities that reached more than 38 °C. Jaipur (41) and Delhi (39) remain; Pune is filtered out.
WITH temperatures(reading_id, city, temperature) AS (
VALUES
(1, 'Delhi', 34),
(2, 'Delhi', 39),
(3, 'Pune', 28),
(4, 'Pune', 32),
(5, 'Jaipur', 36),
(6, 'Jaipur', 41)
)
SELECT
city,
MAX(temperature) AS highest_temperature
FROM temperatures
GROUP BY city
HAVING MAX(temperature) > 38
ORDER BY highest_temperature DESC;
HAVING with Multiple Conditions
You can combine several aggregate conditions with AND or OR.
What this code does: Keeps only stores that have at least two orders and whose total sales exceed 2000. Central (2800) and North (2800) remain; West is filtered out.
WITH store_orders(order_id, store, amount) AS (
VALUES
(1, 'Central', 900),
(2, 'Central', 1200),
(3, 'Central', 700),
(4, 'West', 500),
(5, 'West', 600),
(6, 'North', 1500),
(7, 'North', 1300)
)
SELECT
store,
COUNT(*) AS order_count,
SUM(amount) AS total_sales
FROM store_orders
GROUP BY store
HAVING COUNT(*) >= 2
AND SUM(amount) > 2000
ORDER BY total_sales DESC;
Combining WHERE and HAVING
WHERE removes unwanted rows first; HAVING then filters the groups that remain.
What this code does: First discards transactions under 500, then groups the rest by department and keeps only departments whose remaining total exceeds 2000. Games (2900) and Books (2300) remain.
WITH transactions(transaction_id, department, amount) AS (
VALUES
(1, 'Books', 300),
(2, 'Books', 800),
(3, 'Books', 1500),
(4, 'Games', 400),
(5, 'Games', 1100),
(6, 'Games', 1800),
(7, 'Clothing', 900)
)
SELECT
department,
SUM(amount) AS total_sales
FROM transactions
WHERE amount >= 500
GROUP BY department
HAVING SUM(amount) > 2000
ORDER BY total_sales DESC;
HAVING after Grouping by Multiple Columns
When you group by more than one column, HAVING still works on each unique combination.
What this code does: Groups sales by region and category, then keeps only those combinations whose total is at least 1400. Several high-value region-category pairs remain.
WITH regional_sales(sale_id, region, category, amount) AS (
VALUES
(1, 'North', 'Books', 800),
(2, 'North', 'Books', 700),
(3, 'North', 'Games', 500),
(4, 'South', 'Books', 1200),
(5, 'South', 'Books', 900),
(6, 'South', 'Games', 400),
(7, 'West', 'Games', 1400),
(8, 'West', 'Games', 900)
)
SELECT
region,
category,
SUM(amount) AS total_sales
FROM regional_sales
GROUP BY region, category
HAVING SUM(amount) >= 1400
ORDER BY total_sales DESC;
HAVING with SUM()
SUM is one of the most frequent partners of HAVING when you need groups whose total value meets a threshold.
What this code does: Adds the revenue of every channel for each month and keeps only months whose total is at least 3000. February (3300) and January (3000) remain; March is filtered out.
WITH monthly_revenue(month_name, channel, revenue) AS (
VALUES
('January', 'Online', 1800),
('January', 'Store', 1200),
('February', 'Online', 2400),
('February', 'Store', 900),
('March', 'Online', 1500),
('March', 'Store', 1700)
)
SELECT
month_name,
SUM(revenue) AS total_revenue
FROM monthly_revenue
GROUP BY month_name
HAVING SUM(revenue) >= 3000
ORDER BY total_revenue DESC;
HAVING with COUNT(DISTINCT …)
You can also filter on the number of distinct values inside each group.
What this code does: Counts how many unique customers each salesperson has served and keeps only those who have served at least two different customers. Both Neha and Rohan qualify.
WITH customer_orders(order_id, salesperson, customer) AS (
VALUES
(1, 'Neha', 'Aarav'),
(2, 'Neha', 'Meera'),
(3, 'Neha', 'Aarav'),
(4, 'Rohan', 'Kabir'),
(5, 'Rohan', 'Tara'),
(6, 'Rohan', 'Dev')
)
SELECT
salesperson,
COUNT(DISTINCT customer) AS unique_customers
FROM customer_orders
GROUP BY salesperson
HAVING COUNT(DISTINCT customer) >= 2
ORDER BY unique_customers DESC;
HAVING followed by ORDER BY
After HAVING has filtered the groups you can still sort the remaining result with ORDER BY.
What this code does: Calculates total expenses per department, keeps those whose total is at least 1500, and sorts them from highest to lowest expense. Engineering (3300) and Design (1600) remain.
WITH department_expenses(expense_id, department, amount) AS (
VALUES
(1, 'Design', 700),
(2, 'Design', 900),
(3, 'Engineering', 1500),
(4, 'Engineering', 1800),
(5, 'Marketing', 600),
(6, 'Marketing', 800)
)
SELECT
department,
SUM(amount) AS total_expense
FROM department_expenses
GROUP BY department
HAVING SUM(amount) >= 1500
ORDER BY total_expense DESC;
WHERE vs HAVING
Common Uses of HAVING
- Find categories or departments whose total sales exceed a target.
- Keep only groups that contain a minimum number of rows.
- Filter groups by average, minimum or maximum value.
- Combine several aggregate conditions with AND / OR.
- Produce clean summary reports after aggregation.
Best Practices
- Use WHERE for row-level filtering and HAVING for group-level filtering.
- Always place HAVING after GROUP BY.
- Give clear aliases to aggregate expressions so the HAVING clause stays readable.
- You may refer to column aliases in ORDER BY, but not always in HAVING (depending on the database).
- Combine WHERE + GROUP BY + HAVING + ORDER BY in that logical order for the cleanest queries.
The HAVING clause filters groups after aggregation has taken place. It is the natural partner of GROUP BY and the aggregate functions COUNT, SUM, AVG, MIN and MAX. Click Test My Code on any example to run it live, or Code Explanation to walk through the query block by block.
🧪 Test Your SQL Code
Edit the SQL code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.