SQL — Having
Basic HAVING
SELECT department, COUNT(*) AS emp_count
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;
HAVING with aggregate
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
HAVING AVG(salary) > 50000;
WHERE vs HAVING
-- WHERE filters before grouping
SELECT department, COUNT(*)
FROM employees
WHERE status = 'active'
GROUP BY department;
-- HAVING filters after grouping
SELECT department, COUNT(*)
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;
HAVING with multiple conditions
SELECT department, COUNT(*), AVG(salary)
FROM employees
GROUP BY department
HAVING COUNT(*) > 3 AND AVG(salary) > 40000;
Mini Practice
Write SQL code that:
- Filters groups with HAVING
- Uses aggregate functions in HAVING
- Combines WHERE and HAVING
- Uses multiple conditions in HAVING
Up Next
In the next lesson, you'll learn about Like — pattern matching.
Related Topics
Frequently Asked Questions about Having
What is Having in SQL?
Having is a fundamental concept in SQL. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Having?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Having.
Why is Having important in SQL?
Having is essential for SQL development. Understanding this concept will help you write better code and solve real-world problems more effectively.