</>
Skip to content
PostgreSQL lessons (18/38)

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

FeatureWHEREHAVING
FiltersIndividual rowsGroups
AggregatesNoYes
PositionBefore GROUP BYAfter GROUP BY

Combined

SELECT country, COUNT(*) 
FROM users 
WHERE active = true 
GROUP BY country 
HAVING COUNT(*) > 5;

Mini Practice

  1. Filter groups
  2. Use multiple conditions
  3. Combine with WHERE
  4. 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.