MyMisPal
← SQL lessons
Lesson 6 of 12

GROUP BY & HAVING

GROUP BY splits rows into groups by a column and applies the aggregate to each group. “How many employees per department?” is exactly a GROUP BY.

SQL
SELECT dept_id, COUNT(*) AS staff, AVG(salary) AS avg_salary
FROM employees
GROUP BY dept_id;

WHERE filters rows before grouping; HAVING filters groups after grouping:

SQL
SELECT status, COUNT(*) AS orders
FROM orders
GROUP BY status
HAVING COUNT(*) >= 3;
Watch outKey rule: every column in SELECT without an aggregate must appear in GROUP BY.

Exercises

Exercise 1

Show each category and how many products it has.

Exercise 2

Show dept_id and total salary for departments whose total salary exceeds 30000.

Spotted a mistake on this page, or have an idea to improve the site?