HAVING Clause in SQL

Level 392 keystrokes

HAVING filters groups after GROUP BY, similar to WHERE but for aggregated results.

SQL code

1SELECT
2 department,
-- COUNT(*) gives the number of employees per department
3 COUNT(*) AS count
4FROM
5 employees
-- GROUP BY creates one group per department
6GROUP BY
7 department
-- HAVING filters groups after aggregation (WHERE filters rows before it)
-- Only departments with more than 3 employees are kept
8HAVING
9 COUNT(*) > 3;

Output

+------------+-------+
| department | count |
+------------+-------+
| Sales      |     5 |
| IT         |     8 |
+------------+-------+
2 rows in set
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this SQL code and how often they appear.

  • (× 2
  • )× 2
  • *× 2
  • ,× 1
  • ;× 1
  • >× 1

More SQL examples at level 3

← SQL typing practice