The HAVING clause helps you filter groups of data after you have grouped them. It works like a filter but for groups, not individual rows.
HAVING clause for filtering groups in SQL
Start learning this pattern below
Jump into concepts and practice - no test required
SELECT column1, aggregate_function(column2) FROM table_name GROUP BY column1 HAVING condition;
SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 3;
SELECT product_id, SUM(quantity) FROM sales GROUP BY product_id HAVING SUM(quantity) >= 100;
SELECT city, AVG(salary) FROM employees GROUP BY city HAVING AVG(salary) > 50000;
This query finds customers who made more than 2 orders and shows how many orders and total amount they spent.
CREATE TABLE orders ( order_id INT, customer_id INT, amount DECIMAL(10,2) ); INSERT INTO orders VALUES (1, 101, 250.00), (2, 102, 150.00), (3, 101, 300.00), (4, 103, 50.00), (5, 102, 200.00), (6, 101, 100.00); SELECT customer_id, COUNT(*) AS order_count, SUM(amount) AS total_amount FROM orders GROUP BY customer_id HAVING COUNT(*) > 2;
HAVING filters groups after aggregation, unlike WHERE which filters rows before grouping.
You must use GROUP BY with HAVING; otherwise, HAVING acts like WHERE but is less efficient.
HAVING filters groups created by GROUP BY.
Use aggregate functions in HAVING conditions.
HAVING helps answer questions about groups, like totals or averages.
Practice
What is the main purpose of the HAVING clause in SQL?
Solution
Step 1: Understand the role of
TheGROUP BYGROUP BYclause groups rows based on column values.Step 2: Identify the purpose of
HAVINGHAVINGfilters these groups using aggregate functions likeSUMorCOUNT.Final Answer:
To filter groups created byGROUP BYbased on aggregate conditions -> Option CQuick Check:
HAVINGfilters groups, not rows [OK]
- Confusing HAVING with WHERE clause
- Using HAVING without GROUP BY
- Thinking HAVING sorts data
Which of the following is the correct syntax to filter groups with HAVING?
SELECT department, COUNT(*) FROM employees GROUP BY department _______ COUNT(*) > 5;
Solution
Step 1: Identify filtering clause after grouping
AfterGROUP BY, filtering groups requiresHAVING, notWHERE.Step 2: Confirm correct clause usage
HAVING COUNT(*) > 5filters groups with more than 5 employees.Final Answer:
HAVING -> Option DQuick Check:
Use HAVING after GROUP BY [OK]
- Using WHERE instead of HAVING after GROUP BY
- Placing HAVING before GROUP BY
- Using FILTER keyword which is invalid here
Given the table sales with columns region and amount, what will this query return?
SELECT region, SUM(amount) FROM sales GROUP BY region HAVING SUM(amount) > 1000;
Solution
Step 1: Group sales by region and sum amounts
The query groups rows byregionand calculates totalamountper region.Step 2: Filter groups with total sales > 1000
TheHAVINGclause keeps only regions where the sum is greater than 1000.Final Answer:
All regions with total sales greater than 1000 -> Option AQuick Check:
HAVING filters groups by aggregate sum [OK]
- Thinking HAVING filters individual rows
- Confusing SUM(amount) with amount column
- Assuming syntax error with HAVING
Identify the error in this query:
SELECT category, COUNT(*) FROM products HAVING COUNT(*) > 10 GROUP BY category;
Solution
Step 1: Check order of clauses in SQL
The correct order isGROUP BYfirst, thenHAVING.Step 2: Identify incorrect clause order
The query placesHAVINGbeforeGROUP BY, causing syntax error.Final Answer:
HAVING clause used before GROUP BY -> Option BQuick Check:
GROUP BY before HAVING [OK]
- Placing HAVING before GROUP BY
- Thinking HAVING needs WHERE before it
- Replacing GROUP BY with ORDER BY incorrectly
You have a students table with columns class and score. You want to find classes where the average score is at least 75 and the number of students is more than 10. Which query achieves this?
Solution
Step 1: Group students by class
UseGROUP BY classto group rows by class.Step 2: Filter groups with HAVING using aggregate conditions
UseHAVING AVG(score) >= 75 AND COUNT(*) > 10to keep classes meeting both conditions.Final Answer:
SELECT class, AVG(score), COUNT(*) FROM students GROUP BY class HAVING AVG(score) >= 75 AND COUNT(*) > 10; -> Option AQuick Check:
HAVING filters groups by multiple aggregates [OK]
- Placing HAVING before GROUP BY
- Using WHERE with aggregate functions
- Putting WHERE after GROUP BY
