Sobes.tech
Junior — Middle

Какое назначение у оператора HAVING в SQL и в чем его отличие от WHERE?

sobes.tech AI

Answer from AI

The HAVING operator in SQL is used to filter results after grouping data with GROUP BY. It allows setting conditions for aggregated data.

Difference from WHERE:

  • WHERE filters rows before grouping, working with individual records.
  • HAVING filters groups after aggregation.

Example:

SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department
HAVING COUNT(*) > 10;

Here, WHERE cannot be used to filter by COUNT(*), as it is an aggregate function, whereas HAVING allows selecting only those departments with more than 10 employees.

Какое назначение у оператора HAVING в SQL и в чем его… - sobes.tech