SQL COUNT / SUM / AVG / MIN / MAX

Aggregate functions perform calculations on multiple rows and return a single value โ€” ideal for reports, summaries, and analysis.

๐Ÿ”น Syntax & Examples

-- โœ… COUNT: Total number of rows (non-null values in a column)
SELECT COUNT(*) FROM employees;
SELECT COUNT(salary) FROM employees WHERE department = 'IT';

-- โœ… SUM: Total of a numeric column
SELECT SUM(salary) FROM employees WHERE department = 'HR';

-- โœ… AVG: Average of a numeric column
SELECT AVG(salary) FROM employees;

-- โœ… MIN / MAX: Lowest / Highest value
SELECT MIN(salary) FROM employees;
SELECT MAX(salary) FROM employees;

๐Ÿ”น With GROUP BY

-- Average salary per department
SELECT department, AVG(salary)
FROM employees
GROUP BY department;

-- Count of employees in each department
SELECT department, COUNT(*)
FROM employees
GROUP BY department;

๐Ÿง  Quick Recap

FunctionDescription
COUNTCount rows (or non-null values)
SUMTotal of values
AVGAverage value
MINSmallest value
MAXLargest value

โœ… Use these to summarize and analyze your data