Skip to content
We areTOP 1%
DBMS & SQL · Beginner · Topic 5 of 5

SQL: Aggregation & GROUP BY

5 questions.
Your progress0%
1st Visit
0/5
2nd Visit
0/5
Revise
0/5

Overview

Aggregate functions such as COUNT, SUM, AVG, MIN and MAX turn many rows into one value. GROUP BY splits the rows into groups that share the same values, and each aggregate is computed once per group. WHERE filters rows before grouping, while HAVING filters groups after the aggregates are computed, so a condition such as COUNT(*) > 5 belongs in HAVING. Every non-aggregated column in SELECT should also appear in GROUP BY. Aggregates ignore NULL values, so COUNT(col) can be smaller than COUNT(*). Look for phrases such as "per customer", "for each department", "at least n" or "more than once".

SELECT dept, COUNT(*) AS staff, AVG(salary) AS avg_salary
FROM employees
WHERE active = 1          -- row filter
GROUP BY dept
HAVING COUNT(*) >= 5;     -- group filter

Questions

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account