Suggest an editImprove this articleRefine the answer for “What does HAVING do? How does HAVING differ from WHERE?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)`HAVING` is used to **filter results after grouping** (`GROUP BY`), while `WHERE` is used to **filter rows before grouping**: `WHERE` runs **before** `GROUP BY` and selects individual rows, `HAVING` runs **after** and selects **groups** based on the results of aggregate functions. **Key point:** in `GROUP BY department HAVING SUM(salary) > 100000`, groups are formed by department first, then only the ones where the salary sum exceeds 100,000 remain; using `WHERE` instead of `HAVING` here would fail, since SQL wouldn't understand `SUM(salary)` at that stage, as aggregates haven't been computed yet.Shown above the full answer for quick recall.Answer (EN)Image`HAVING` is used to **filter results after grouping** (`GROUP BY`). `WHERE`, on the other hand, is used to **filter rows before grouping**. ### The difference in execution order - `WHERE` runs **before** `GROUP BY`, selecting individual rows; - `HAVING` runs **after**, selecting **groups** based on the results of aggregate functions. ### Example ```sql SELECT department, SUM(salary) FROM employees GROUP BY department HAVING SUM(salary) > 100000; ``` Groups by `department` are formed first, then only the ones where the salary sum exceeds 100,000 remain. If `WHERE` were used instead of `HAVING`, SQL wouldn't understand `SUM(salary)`, because aggregates haven't been computed at that stage yet.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.