Sobes.tech
Junior — Middle

How is the syntax for filtering grouped data using HAVING in SQL implemented?

sobes.tech AI

Answer from AI

The SQL HAVING operator is used to filter grouped data, that is, after applying GROUP BY. Unlike WHERE, which filters rows before grouping, HAVING filters already aggregated groups.

Syntax:

SELECT column1, AGG_FUNC(column2)
FROM table_name
GROUP BY column1
HAVING AGG_FUNC(column2) condition;

For example, to select customers with a total order amount greater than 1000:

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

Thus, HAVING allows setting conditions on aggregate functions after grouping.

How is the syntax for filtering grouped data using… - sobes.tech