SQL Programmers Blog - SQL Aggregate Functions


  

Need help with Microsoft SQL Server? Ask our Experts, or simply Email your query.

About Sql Server Programmers

Our clients can be found both locally in the Chicagoland area and throughout the country. We have over 18 years of experience and are extremely proud of our track record of successfully assisting hundreds of our clients to improve their productivity while focusing on cost.

Microsoft Certified Partner

  

SQL Server Programmers Blog

Jul 2

Written by: host
7/2/2010 2:42 AM 

SQL Server provides many built-in functions that operate on a set of input values and result in a single output value. In this article, we are going to look at commonly used aggregate functions. SQL Aggregate functions can be easily understood through examples. Here is a simple example table that will be used through out this article to demonstrate SQL Aggregate functions.

Let’s consider the salary of the employees in an organization.

SQL Aggregate

COUNT

This function returns the number of rows in a selection or group.

Syntax:

COUNT ( [ ALL | DISTINCT ]  column_name ) or COUNT( *)

ALL – when specified, counts all items including duplicate values.
DISTINCT – when specified, ignores the duplicate items and returns the count of unique items.
* (asterisk) – when specified, returns the number of records in the table

For example, to return the number of employees working in the organization,

SQL Aggregate

Let’s look at the difference between when the optional keywords ALL and DISTINCT are specified. For example, to get the number of departments in the organization,

SQL Aggregate

From the above example, when the DISTINCT keyword is mentioned, the COUNT() returns the number of unique items in the department column. Now let’s see how the above example works if the DISTINCT keyword is replaced by the ALL keyword.

SQL Aggregate

It counts all of the items in the department column including the duplicate items, thus it returns 8.

COUNT_BIG

This function works in the same way as COUNT () works. The COUNT_BIG differs only in the data type of the return value. It always returns the count in the bigint data type.

Syntax:

COUNT_BIG ( [ ALL | DISTINCT ]  column_name ) or COUNT_BIG ( *)

SUM

This function returns the sum of all the values in the given numeric column.

Syntax:

SUM([ALL | DISTINCT] column_name)

For example, to calculate the total salary of all employees in the tblEmployees table:

 SQL Aggregate

Now, to calculate the total salary of each department:

SQL Aggregate SUM

MAX

This function returns the maximum value in the given numeric column.

Syntax:

MAX (    [ ALL | DISTINCT ] column_name )

For example, to find out the highest salary paid to the employees in the company:

SQL Aggregate

Next, to evaluate the highest salary paid within each department of the company,

SQL Aggregate

MIN

This function returns the minimum value in the given numeric column.

Syntax:

MIN (    [ ALL | DISTINCT ] column_name )

For example, to calculate the lowest salary paid in the company,

SQL Aggregate

Now, to calculate the lowest salary paid within each department of the company,

SQL Aggregate

AVG

This function returns the average of all the values in the given numeric column.

Syntax:

AVG ( [ ALL | DISTINCT ] column_name )

For example, to calculate the avearge salary of the employees in the company:

SQL Aggregate

To calculate the average salary of each department in the company:

SQL Aggregate

Tags:

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel