Last Updated : 02 Aug, 2025
SQL Aggregate Functions are used to perform calculations on a set of rows and return a single value. These functions are particularly useful when we need to summarize, analyze, or group large datasets in SQL databases.
They are often used with the GROUP BY clause in SQL to summarize data for each group. Commonly used aggregate functions include COUNT(), SUM(), AVG(), MIN() and MAX().
Key Features of SQL Aggregate Functions:COUNT(*)
.GROUP BY
: To perform calculations on grouped data, you often use aggregate functions with GROUP BY
.HAVING
, ORDER BY
and other SQL clauses to filter or sort results.Below are the most frequently used aggregate functions in SQL.
1. Count()The COUNT() function returns the number of rows that match a given condition or are present in a column.
Examples:
2. SUM()-- Total number of records in the table
SELECT COUNT(*) AS TotalRecords FROM Employee;-- Count of non-NULL salaries
SELECT COUNT(Salary) AS NonNullSalaries FROM Employee;-- Count of unique non-NULL salaries
SELECT COUNT(DISTINCT Salary) AS UniqueSalaries FROM Employee;
The SUM() function calculates the total sum of a numeric column.
SUM(column_name): Returns the total sum of all non-NULL values in a column.
Examples:
3. AVG()-- Calculate the total salary
SELECT SUM(Salary) AS TotalSalary FROM Employee;-- Calculate the sum of unique salaries
SELECT SUM(DISTINCT Salary) AS DistinctSalarySum FROM Employee;
The AVG() function calculates the average of a numeric column. It divides the sum of the column by the number of non-NULL rows.
AVG(column_name): Returns the average of the non-NULL values in the column.
Examples:
4. MIN() and MAX()-- Calculate the average salary
SELECT AVG(Salary) AS AverageSalary FROM Employee;-- Average of distinct salaries
SELECT AVG(DISTINCT Salary) AS DistinctAvgSalary FROM Employee;
The MIN() and MAX() functions return the smallest and largest values, respectively, from a column.
Examples:
Examples of SQL Aggregate Functions-- Find the highest salary
SELECT MAX(Salary) AS HighestSalary FROM Employee;-- Find the lowest salary
SELECT MIN(Salary) AS LowestSalary FROM Employee;
Let's consider a demo Employee table to demonstrate SQL aggregate functions . This table contains employee details such as their ID, Name, and Salary.
Id Name Salary 1 A 802 2 B 403 3 C 604 4 D 705 5 E 606 6 F NULL 1. Count the Total Number of EmployeesSELECT COUNT(*) AS TotalEmployees FROM Employee;
Output:
2. Calculate the Total SalarySELECT SUM(Salary) AS TotalSalary FROM Employee;
Output:
3. Find the Average Salary:SELECT AVG(Salary) AS AverageSalary FROM Employee;
Output:
SELECT MAX(Salary) AS HighestSalary FROM Employee;
Output:
Using Aggregate Functions with GROUP BYSQL GROUP BY allows us to group rows that have the same values in specific columns. We can then apply aggregate functions to these groups, which helps us summarize data for each group. This is commonly used with the COUNT(), SUM(), AVG(), MIN(), and MAX() functions.
Example: Total Salary by Each EmployeeSELECT Name, SUM(Salary) AS TotalSalary
FROM Employee
GROUP BY Name;
Output:
Name TotalSalary A 802 B 403 C 604 D 705 E 606 F - Using HAVING with Aggregate FunctionsThe HAVING clause is used to filter results after applying aggregate functions, unlike WHERE, which filters rows before aggregation. HAVING is essential when we want to filter based on the result of an aggregate function.
Example: Find Employees with Salary Greater Than 600SELECT Name, SUM(Salary) AS TotalSalary
FROM Employee
GROUP BY Name
HAVING SUM(Salary) > 600;
Output:
Name TotalSalary A 802 C 604 D 705 E 606Aggregate Functions and GROUP BY Statement in SQL
Visit CourseRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4