Aggregate Function
An aggregate function computes a single value from a set of rows — COUNT, SUM, AVG, MIN, and MAX. With GROUP BY it produces one result per group.
Aggregates collapse many rows into one summary value. GROUP BY splits rows into groups first, so SELECT department_id, AVG(salary) ... GROUP BY department_id returns one average per department.
Filter groups with HAVING, not WHERE: WHERE filters rows before grouping, HAVING filters the aggregated groups afterward. Most aggregates ignore NULL values.
Example
SELECT department_id, COUNT(*) AS headcount, AVG(salary) AS avg_salary
FROM employees
GROUP BY department_id
HAVING COUNT(*) > 5;