SQLIntermediate#aggregates

Difference between WHERE and HAVING?

WHERE filters individual rows before grouping and cannot use aggregate functions. HAVING filters groups produced by GROUP BY and can use aggregates.

Example
SELECT dept_id, COUNT(*) AS total
FROM employees
WHERE active = 1        -- row filter
GROUP BY dept_id
HAVING COUNT(*) > 5;    -- group filter

Related Questions

1
SQLIntermediate#queries

What is the logical order of execution of a SELECT query?

Open
2
SQLBeginner#keys

Primary key vs unique key vs foreign key?

Open
3
SQLBeginner#keys

What is a composite key?

Open