Sobes.tech
Junior — Middle

Selgitage HAVING operaatori ja WHERE tingimuse erinevusi SQL-is ja millal neid kasutada.

sobes.tech AI

Vastus AI-lt

In SQL, the WHERE condition filters rows before grouping, while HAVING does so after.

  • WHERE applies to individual table rows and limits the dataset that will be aggregated.
  • HAVING applies to the results of aggregate functions and filters the groups formed by the GROUP BY statement.

Example:

SELECT department, COUNT(*) as employee_count
FROM employees
WHERE salary > 50000  -- filter employees with salary above 50000
GROUP BY department
HAVING COUNT(*) > 10    -- select departments with more than 10 employees

Use WHERE to filter the original data, and HAVING to filter the aggregated results.