T-SQL » Functions » AVG

Syntax:
SELECT AVG ( field ) [ AS expression ]
FROM table
[ WHERE criteria ]
field
The name of the column on which the average of the values is to be taken.
expression
The name to be displayed for the resulting average.
table
The name of the table the information is being retrieved from.
criteria
The condition(s) that dictate which rows are to be averaged.

The AVG function simply calculates the arithmetic mean (i.e., the sum of the values divided by the number of values).

Values which are NULL are not included in the calculations. If the field is just an empty set, no average will be calculated.

Examples

Code:
SELECT AVG(DaysInHospital) AS AverageStay
FROM MediCarePatients
WHERE age > 65;
Output:
AverageStay
71

(1 row(s) affected)
Explanation:

This code finds the average hospital stay of those MediCare patients over the age of 65.

Language(s): MS SQL Server

See Also: