COUNT, SUM, AVG, MIN, MAX in SQL

11/22/2025
All Articles

SQL COUNT SUM AVG MIN MAX aggregate functions tutorial

COUNT, SUM, AVG, MIN, MAX in SQL

COUNT, SUM, AVG, MIN, MAX in SQL – Aggregate Functions Tutorial

SQL aggregate functions allow you to perform calculations on groups of rows and return a single value. These functions are essential when working with reports, analytics, dashboards, and summary data.

In this SQL tutorial, you will learn:

  • What aggregate functions are

  • COUNT(), SUM(), AVG(), MIN(), MAX() explained with examples

  • GROUP BY with aggregates

  • Real-world use cases

  • Best practices


🔹 What Are Aggregate Functions?

Aggregate functions perform calculations on multiple rows and return one result.

Most commonly used aggregate functions are:

  • COUNT() – counts rows

  • SUM() – adds values

  • AVG() – calculates average

  • MIN() – finds minimum value

  • MAX() – finds maximum value

Used together with:

  • SELECT

  • GROUP BY

  • HAVING


Example Table: employees

id name department salary
1 Amit IT 50000
2 Neha HR 45000
3 Rahul IT 60000
4 Sara HR 55000
5 Karan Finance 70000

1️⃣ COUNT() – Count Rows

Counts the number of rows in a table or column.

Count total employees:

SELECT COUNT(*) AS total_employees
FROM employees;

Count employees in IT:

SELECT COUNT(*) AS it_employees
FROM employees
WHERE department = 'IT';

2️⃣ SUM() – Add Values

Calculates total of numeric columns.

Total salary of all employees:

SELECT SUM(salary) AS total_salary
FROM employees;

Total salary of HR department:

SELECT SUM(salary) AS hr_salary
FROM employees
WHERE department = 'HR';

3️⃣ AVG() – Average Value

Finds the average of numeric values.

Average salary:

SELECT AVG(salary) AS average_salary
FROM employees;

Average salary in IT:

SELECT AVG(salary) AS it_average
FROM employees
WHERE department = 'IT';

4️⃣ MIN() – Minimum Value

Finds the smallest value in a column.

Minimum salary:

SELECT MIN(salary) AS minimum_salary
FROM employees;

5️⃣ MAX() – Maximum Value

Finds the highest value in a column.

Maximum salary:

SELECT MAX(salary) AS maximum_salary
FROM employees;

🔹 Using Aggregate Functions with GROUP BY

Group data and apply aggregate functions.

Example: Total salary by department

SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department;

Example: Count employees per department

SELECT department, COUNT(*) AS total_employees
FROM employees
GROUP BY department;

Example: Average salary per department

SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;

🔹 Using Aggregate Functions with HAVING

Filter aggregated results.

Example: Departments with more than 1 employee

SELECT department, COUNT(*) AS total_employees
FROM employees
GROUP BY department
HAVING COUNT(*) > 1;

🔹 Real-World Examples

✔ Sales Analytics

SELECT product_id, SUM(quantity) AS total_sold
FROM sales
GROUP BY product_id;

✔ Customer Orders Summary

SELECT customer_id, COUNT(*) AS orders
FROM orders
GROUP BY customer_id;

✔ Salary Report

SELECT department, MIN(salary), MAX(salary), AVG(salary)
FROM employees
GROUP BY department;

🔹 Best Practices

✔ Always use GROUP BY when selecting non-aggregated columns
✔ Use aliases for readability (AS total_salary)
✔ Index columns used in GROUP BY for faster performance
✔ Use HAVING for aggregated conditions after grouping

 Summary

In this SQL tutorial, you learned:

  • How COUNT, SUM, AVG, MIN, MAX work

  • How to summarize data using aggregate functions

  • How to group results using GROUP BY

  • Real-world examples used in reporting and analytics

Aggregate functions are essential for business intelligence, dashboards, and database reporting.

Article