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
| Function | Description |
|---|---|
COUNT | Count rows (or non-null values) |
SUM | Total of values |
AVG | Average value |
MIN | Smallest value |
MAX | Largest value |
โ
Use these to summarize and analyze your data