Aggregate Functions in SQL (COUNT, SUM, AVG, MIN, MAX)
Introduction
Aggregate functions in SQL are essential for data analysis because they allow you to summarize and analyze large datasets quickly. Instead of viewing individual records, you can calculate totals, averages, and other insights. In this lesson, you will learn how to use aggregate functions in SQL for data analysis.
What are Aggregate Functions in SQL
Aggregate functions perform calculations on multiple rows and return a single value. They are widely used in reporting, dashboards, and business analytics.
Common aggregate functions include:
- COUNT()
- SUM()
- AVG()
- MIN()
- MAX()
COUNT Function in SQL
The COUNT() function is used to count the number of rows in a table.
Example:
SELECT COUNT(*) FROM employees;
This query returns the total number of employees.
You can also count specific columns:
SELECT COUNT(department) FROM employees;
SUM Function in SQL
The SUM() function calculates the total value of a numeric column.
Example:
SELECT SUM(salary) FROM employees;
This returns the total salary of all employees.
AVG Function in SQL
The AVG() function calculates the average value of a column.
Example:
SELECT AVG(salary) FROM employees;
This helps in analyzing average salary or performance metrics.
MIN Function in SQL
The MIN() function returns the smallest value in a column.
Example:
SELECT MIN(salary) FROM employees;
MAX Function in SQL
The MAX() function returns the highest value in a column.
Example:
SELECT MAX(salary) FROM employees;
Using Aggregate Functions with WHERE
You can combine aggregate functions with WHERE to filter data.
Example:
SELECT COUNT(*) FROM employees WHERE department = ‘IT’;
This counts employees only from the IT department.
Why Aggregate Functions are Important in Data Analysis
Aggregate functions help you:
- Summarize large datasets
- Generate business insights
- Create reports and dashboards
- Analyze trends and performance
Best Practices
- Use aggregate functions with filtering for accurate insights
- Avoid unnecessary calculations on large datasets
- Combine with GROUP BY for detailed analysis
- Use clear column names for readability
Summary
In this lesson, you learned how to use aggregate functions like COUNT, SUM, AVG, MIN, and MAX in SQL. These functions are fundamental for data analysis and help you extract meaningful insights from large datasets. In the next lesson, you will learn how to group data using GROUP BY.
FAQs
1. What are aggregate functions in SQL?
They are functions used to perform calculations on multiple rows and return a single value.
2. Which aggregate function is most used?
COUNT() is one of the most commonly used aggregate functions.
3. Can aggregate functions be used with WHERE?
Yes, they can be combined to filter data before calculation.
4. Do aggregate functions work on text columns?
Most aggregate functions work on numeric data, but COUNT can be used on text columns.
Internal Link
Want to explore more courses?
Click here for more free courses



