Sobes.tech
Junior — Middle

Explain the purpose of the HAVING clause in SQL and how it is used to filter grouping results.

sobes.tech AI

Answer from AI

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

For example, if you want to select groups with a total sales amount greater than 1000, HAVING allows you to filter such groups:

SELECT customer_id, SUM(amount) as total_sales
FROM orders
GROUP BY customer_id
HAVING total_sales > 1000;

Thus, HAVING allows setting conditions on aggregate functions (SUM, COUNT, AVG, etc.) after grouping.

Explain the purpose of the HAVING clause in SQL and… - sobes.tech