PostgreSQL — Having
Basic Having
SELECT country, COUNT(*)
FROM users
GROUP BY country
HAVING COUNT(*) > 10;
With Multiple Conditions
SELECT country, COUNT(*)
FROM users
GROUP BY country
HAVING COUNT(*) > 10 AND COUNT(*) < 100;
With Aggregate Functions
SELECT department, AVG(salary)
FROM employees
GROUP BY department
HAVING AVG(salary) > 50000;
Where vs Having
| Feature | WHERE | HAVING |
|---|---|---|
| Filters | Individual rows | Groups |
| Aggregates | No | Yes |
| Position | Before GROUP BY | After GROUP BY |
Combined
SELECT country, COUNT(*)
FROM users
WHERE active = true
GROUP BY country
HAVING COUNT(*) > 5;
Mini Practice
- Filter groups
- Use multiple conditions
- Combine with WHERE
- Compare WHERE and HAVING
Up Next
Continue with Joins — joining tables.
Related Topics
Frequently Asked Questions about Having
What is Having in PostgreSQL?
Having is a fundamental concept in PostgreSQL. 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 PostgreSQL?
Having is essential for PostgreSQL development. Understanding this concept will help you write better code and solve real-world problems more effectively.