Intermediate · Lesson 5 of 20

Filtering Groups with HAVING

HAVING filters groups (like WHERE filters rows).

Find departments with more than 3 employees:

SELECT department_id, COUNT(*)
FROM employees
GROUP BY department_id
HAVING COUNT(*) > 3;

WHERE vs HAVING:

  • WHERE filters BEFORE grouping
  • HAVING filters AFTER grouping

HAVING works with aggregate functions.

Check your understanding

Find departments with more than 2 employees.

Examples run on the employees sample database. Open any of them in the playground to experiment.

Practice it on a real question

All Aggregation questions

Practice on your iPhone

Query Dojo for iPhone has 85 interview questions, timed mock interviews and a tile builder, so you can write queries without a keyboard.

Get the app