SQL Server > Aggregate functions

Aggregate functions like AVG, MIN, MAX, SUM in SQL Server

How to user aggregate functions like AVG, MIN, MAX, SUM etc in SQL Server?


As AVG, MIN, MAX, and SUM all these functions work on numeric data so we need to pass numeric data type of columns to these functions that returns a single value.

SELECT SUM(Salary) TotalSalary,
             AVG(NetSalary) AverageSalary,
             MIN(Salary) LowestSalary,
             MAX(Salary) HighestSalary
      FROM Accounts

In above query, we are getting the sum of Salary, average of NetSalary, the lowest of the salary and highest of the salary.

We can also filter the data and get these values based on the filtered records.

SELECT SUM(Salary) TotalSalary,
             AVG(NetSalary) AverageSalary,
             MIN(Salary) LowestSalary,
             MAX(Salary) HighestSalary
      FROM Accounts
WHERE AutoId > 3

This returns sum, average, min and max of only those records whose AutoId value is greater than 3.

 Views: 10323 | Post Order: 69



Write for us






Hosting Recommendations