SQL AVG Function

Friday, December 09, 2011

tags SQL

The SQL AVG() function returns the average value of a table's column/field.

Employees Table

employeeIDemployeeNameage
1000John Smith40
1001Fred White27
1002Jane Scott53
1003Samuel Williams31


In this example, we want to find the average age of an employee.


Syntax

SELECT AVG(column_name)
FROM Table


Example
SELECT AVG(age) as [Average Age]
FROM employees

Or

SELECT employeeID, employeeName FROM employees
WHERE age > (SELECT AVG(age) from employees)


Results

Average Age
37


employeeIDemployeeName
1000John Smith
1002Jane Scott

The SQL AVG function can return the average of a column or can be used with WHERE to filter the rows for the specific range of data that you need.

Did you find the page informational and useful? Share it using one of your favorite social sites.

Recommended Books & Training Resources

Head First SQL: Your Brain on SQL A Learners Guide SQL Cookbook