Back to PostgreSQL Notes
Topic #38

PostgreSQL HAVING


HAVING

The HAVING clause was added to SQL because the WHERE clause cannot be used with aggregate functions.

Aggregate functions are often used with GROUP BY clauses, and by adding HAVING we can write condition like we do with WHERE clauses.

Example

SELECT COUNT(customer_id), country
FROM customers
GROUP BY country
HAVING COUNT(customer_id) > 5;

More HAVING Examples

The following SQL statement lists only orders with a total price of 400$ or more:

Example

SELECT order_id, SUM(order_details.quantity * products.price)
FROM order_details
LEFT JOIN products on products.product_id = order_details.product_id
GROUP BY order_id
HAVING SUM(order_details.quantity * products.price) > 400.00;

Lists customers that have ordered for 1000$ or more:

Example

SELECT customers.customer_name,
SUM(order_details.quantity * products.price)
FROM order_details
INNER JOIN products ON order_details.product_id = products.product_id
INNER JOIN orders ON order_details.order_id = orders.order_id
INNER JOIN customers ON orders.customer_id = customers.customer_id
GROUP BY customers.customer_id, customers.customer_name
HAVING SUM(order_details.quantity * products.price) >= 1000;

Want to go beyond the notes?

Join Coding Now Tech Institute's PostgreSQL course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

PostgreSQL HAVING – FAQs

Quick answers about learning PostgreSQL HAVING in PostgreSQL.

This free note from Coding Now Tech Institute explains PostgreSQL HAVING in PostgreSQL — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every PostgreSQL topic on Coding Now Tech Institute, including PostgreSQL HAVING, is 100% free with no signup required.
With focused practice, most students grasp PostgreSQL HAVING in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now