SQL Aggregate functions (original) (raw)

Last Updated : 19 Nov, 2025

SQL Aggregate Functions allow summarizing large sets of data into meaningful results, making it easier to analyze patterns and trends across many records. They return a single output value after processing multiple rows in a table.

**Example: First, we create a demo SQL database and table, on which we use the Aggregate functions.

Screenshot-2025-11-18-124900

Employee Table

**Query:

SELECT SUM(Salary) FROM Employee;

**Output:

Screenshot-2025-11-18-130941

**Syntax:

AGGREGATE_FUNCTION(column_name)

Aggregate Functions in SQL

Below are the most frequently used aggregate functions in SQL.

1. Count()

It is used to count the number of rows in a table. It helps summarize data by giving the total number of entries. It can be used in different ways depending on what you want to count:

**Query:

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

**Output:

Screenshot-2025-11-18-125708

2. SUM()

It is used to calculate the total of a numeric column. It adds up all non-NULL values in that column for Example, SUM(column_name) returns sum of all non-NULL values in the specified column.

**Query:

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

**Output:

Screenshot-2025-11-18-125840

3. AVG()

It is used to calculate average value of a numeric column. It divides sum of all non-NULL values by the number of non-NULL rows for Example, AVG(column_name) returns average of all non-NULL values in the specified column.

**Query:

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

-- Average of distinct salaries
SELECT AVG(DISTINCT Salary) AS DistinctAvgSalary FROM Employee;

**Output:

Screenshot-2025-11-18-130024

4. MIN() and MAX()

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

**Query:

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

-- Find the lowest salary
SELECT MIN(Salary) AS LowestSalary FROM Employee;

**Output:

Screenshot-2025-11-18-130208

Aggregate Functions and GROUP BY Statement in SQL

Visit Course explore course icon