You are currently viewing Aggregate Functions in SQL Server

Aggregate Functions in SQL Server

Aggregate Functions are built in functions in SQL Server and are applied on set of records. We can perform computation on set of values rather than a single value using aggregate functions. Aggregate function gives the result in a single value.

For Example, we can calculate average salary, minimum salary, maximum salary etc.

For demo, First we create a table tblEmployee  and insert some records and then do some practical examples.

CREATE TABLE tblEmployee
(
	EmpID INT PRIMARY KEY NOT NULL,
	EmpFirstName NVARCHAR(20) NOT NULL,
	EmpLastName NVARCHAR(20) NOT NULL,
	EmpAge INT NULL,
	EmpGender NVARCHAR(10) NULL,
	EmpEmail NVARCHAR(50) NULL,
	EmpSalary INT NULL,
	EmpJoiningDate DATETIME NULL
)
GO
INSERT INTO tblEmployee
    (EmpID, EmpFirstName,EmpLastName,EmpAge,EmpGender,EmpEmail,EmpSalary,EmpJoiningDate)
VALUES
    (1,'john','dave',26,'male','john@gmail.com',26000,'2010-02-01'),
    (2,'kamran','akmal',35,'male','kami@gmail.com',25000,'2011-01-01'),
    (3,'umar','akmal',36,'male','umar@gmail.com',38000,'2015-01-01'),
    (4,'thomas','hardy',36,'male','thomas@gmail.com',25000,'2016-05-05'),
    (5,'amar','sidhu',36,'male','amar@gmail.com',25000,'2016-05-05'),
    (6,'simran','sidhu',25,'female','simran@gmail.com',33000,'2018-01-01'),
    (7,'diyana','jeni',26,'female','diyana@gmail.com',50000,'2005-01-01'),
    (8,'maria','anders',27,'female','maria@gmail.com',70000,'2004-01-01'),
    (9,'anam','chaudhary',29,'female','anam@gmail.com',25000,'2020-01-01'),
    (10,'Amelia','sidhu',56,'female','amelia@gmail.com',50000,'2015-01-01')
    GO
    

Aggregate Function: MIN()

The MIN() function is used to return the minimum value in a column/expression.

Syntax:

SELECT MIN(ColumnName) FROM TableName
WHERE condition[optional];
    

We will apply MIN() function on EmpSalary column to find the minimum salary.

SELECT MIN(EmpSalary) AS MinSalary
FROM tblEmployee;
    

The result of above MIN() function is shown below.

Aggregate Function: MAX()

The MAX() function is used to return the maximum value in a column/expression.

Syntax:

SELECT MAX(ColumnName) FROM TableName
WHERE condition[optional];
    

We will apply MAX() function on EmpSalary column to find the maximum salary.

SELECT MAX(EmpSalary) AS MaxSalary
FROM tblEmployee;
    

The result of above MAX() function is shown below.

Aggregate Function: AVG()

The AVG() function is used to return the average value of a column/expression. The calculation is performed on numeric values.

Syntax:

SELECT AVG(ColumnName) FROM TableName
WHERE condition[optional];
    

We will apply AVG() function on EmpSalary column to find the average salary.

SELECT AVG(EmpSalary) AS AVGSalary
FROM tblEmployee;
    

The result of above AVG() function is shown below.

Aggregate Function: SUM()

The SUM() function is used to return the sum of all values of a column/expression. The calculation is performed on numeric values. WHERE clause can also be used to filter the rows.

Syntax:

SELECT SUM(ColumnName) FROM TableName
WHERE condition[optional];
    

We will apply SUM() function on EmpSalary column to find the average salary. In this demo, we find the SUM of salaries of only male employees by using WHERE clause.

SELECT SUM(EmpSalary) AS SumOfMaleSalaries
FROM tblEmployee
WHERE EmpGender='male';
    

The result of above SUM() function is shown below.

Aggregate Function: COUNT()

The COUNT() function is used to return the number of rows in an expression.

Syntax:

SELECT COUNT(ALL|ColumnName) FROM TableName
WHERE condition[optional];
    

We will apply COUNT() function on all columns to find the COUNT/Number of rows.

SELECT COUNT(*) AS TotalEmployees
FROM tblEmployee;
    

The result of above COUNT() function is shown below.

Recommended Readings