The Data Management Center

SQL Aggregate Functions


David Haertzen David Haertzen, Tutorial Author
Check out our
Data Warehousing Tutorial.

HOME | SQL OVERVIEW | SQL BASICS | SQL ADMINISTRATION | SQL ADVANCED | SQL SYNTAX | Bookmark and Share
infogoal.com HOME
SQL OVERVIEW

SQL BASICS
SQL SELECT
SQL WHERE
SQL INSERT
SQL UPDATE
SQL DELETE

SQL ADMINISTRATION
SQL CREATE DATABASE
SQL DROP DATABASE
SQL CREATE TABLE
SQL ALTER TABLE
SQL DROP TABLE
SQL CREATE INDEX
SQL DROP INDEX
SQL ADD FOREIGN KEY
SQL DROP FOREIGN KEY
SQL CREATE VIEW
SQL DROP VIEW

SQL ADVANCED
SQL CONCAT
SQL SUBSTRING
SQL TRIM
SQL AND & OR
SQL IN
SQL BETWEEN
SQL LIKE
SQL DISTINCT
SQL GROUP BY
SQL AGGREGATE
SQL HAVING
SQL ORDER BY
SQL JOIN
SQL OUTER JOIN

SQL SYNTAX
SQL BOOKREVIEWS

Previous | Next

SQL > SQL Advanced > SQL Aggregate Functions


What are SQL Aggregate Functions?

The SQL Aggregate Functions are functions that provide mathematical operations. If you need to add, count or perform basic statistics, these functions will be of great help.
 

 

The functions include:

  • count() - counts a number of rows
  • sum() - compute sum
  • avg() - compute average
  • min() - compute minimum
  • max() - compute maximum

Why Use SQL Aggregate Functions?

The SQL Aggregate Functions are useful when mathematical operations must be performed on all or a grouping of values.

How To Use SQL Aggregate Functions

SQL Aggregate Functions are used as follows. If a grouping of values is needed also include the GROUP BY clause.

Use a column name or expression as the parameter to the Aggregate Function. The parameter, '*', represents all rows.

SQL Aggregate Functions Syntax

 
SELECT <column_name1>, <column_name2> <aggregate_function(s)>
FROM <table_name>
GROUP BY <column_name1>, <column_name2>
 

SQL Aggregate Functions Example

The following example Aggregate Functions are applied to the employee_count of the branch table. The region_nbr is the level of grouping.

Here are the contents of the table:

Table: BRANCH
branch_nbrbranch_nameregion_nbremployee_count
108New York10010
110Boston1006
212Chicago2005
404San Diego4006
415San Jose4003

This SQL Statement with aggregate functions is executed:

 
SELECT region_nbr, count(branch_nbr), sum(employee_count), min(employee_count),
max(employee_count), avg(employee_count)
FROM dbo.branch
GROUP BY region_nbr
ORDER BY region_nbr
 

Here is the result.

region_nbrcount
(branch_nbr)
sum (employee_count)min
(employee_count)
max
(employee_count)
avg
(employee_count)
1002166108
20015555
40029364

HOME | SQL OVERVIEW | SQL BASICS | SQL ADMINISTRATION | SQL ADVANCED | SQL SYNTAX
Copyright© 1999-2012, First Place Software, Inc.