Sobes.tech
Junior — Middle

Mis on HAVING operaatori ülesanne SQL-is ja kuidas see erineb WHERE-st?

sobes.tech AI

Vastus AI-lt

The HAVING clause 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, works 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, while HAVING allows selecting only those departments with more than 10 employees.