Sobes.tech
Junior — Middle

What is the purpose and use of the HAVING operator in SQL queries?

sobes.tech AI

Answer from AI

The HAVING operator in SQL is used to filter the results of aggregate functions after grouping data with GROUP BY. Unlike WHERE, which filters rows before grouping, HAVING applies to grouped data.

For example, if you want to select customers whose total orders exceed 1000:

SELECT customer_id, SUM(order_amount) AS total
FROM orders
GROUP BY customer_id
HAVING total > 1000;

Here, HAVING filters customer groups based on the sum of orders, whereas WHERE cannot use aggregate functions.

What is the purpose and use of the HAVING operator in… - sobes.tech