A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/sql/aggregate-functions-in-sql/ below:

SQL Aggregate functions - GeeksforGeeks

SQL Aggregate functions

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: Commonly Used SQL Aggregate Functions

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:

-- 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;

2. SUM()

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:

-- 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;

3. AVG()

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:

-- Calculate the average salary
SELECT AVG(Salary) AS AverageSalary FROM Employee;

-- Average of distinct salaries


SELECT AVG(DISTINCT Salary) AS DistinctAvgSalary FROM Employee;

4. MIN() and MAX()

The MIN() and MAX() functions return the smallest and largest values, respectively, from a column.

Examples:

-- Find the highest salary
SELECT MAX(Salary) AS HighestSalary FROM Employee;

-- Find the lowest salary


SELECT MIN(Salary) AS LowestSalary FROM Employee;

Examples of SQL Aggregate Functions

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 Employees

SELECT COUNT(*) AS TotalEmployees FROM Employee;

Output:

2. Calculate the Total Salary

SELECT SUM(Salary) AS TotalSalary FROM Employee;

Output:

3. Find the Average Salary:

SELECT AVG(Salary) AS AverageSalary FROM Employee;


Output:

4. Find the Highest and Lowest Salary

SELECT MAX(Salary) AS HighestSalary FROM Employee;

Output:

Using Aggregate Functions with GROUP BY

SQL 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 Employee

SELECT 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 Functions

The 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 600

SELECT Name, SUM(Salary) AS TotalSalary
FROM Employee
GROUP BY Name
HAVING SUM(Salary) > 600;

Output:

Name TotalSalary A 802 C 604 D 705 E 606

Aggregate Functions and GROUP BY Statement in SQL

Visit Course

RetroSearch 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