SQL COUNT / SUM / AVG / MIN / MAX

ADVERTISEMENT

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

ADVERTISEMENT