SQL AVG() Function
Submitted by admin on Monday, May 9, 2011 - 14:41.
The AVG function calculates the average of a specified column. It works only on a numeric column. It first sums up all data and then divides it by the total number of rows.For example if the sum of 5 items is 10 then average is 10/5 = 2.
SQL AVG() Syntax
SELECT AVG(column_name) FROM TABLE
Consider the following table for this exercise
Users
Firstname | Lastname | Salary | deptnumber |
---|---|---|---|
John | Smith | 1000 | 1 |
Mathew | Simon | 3000 | 1 |
Bill | Steve | 2200 | 1 |
Amanda | Rogers | 1800 | 2 |
Steve | Hills | 2800 | 2 |
Steve | jobs | 2400 | 2 |
bill | cosby | 700 | 3 |
Example # 1
SELECT AVG(Monthly_Salary) AS AverageSalary FROM Users
Result of the Query
AverageSalary |
---|
1985.72 |
Here all the salaries of all users are summed up to a total value of 13,900 and divided by the total number of users (i.e. 7). So the average is calculated as 13,900/7 = 1,985.72
Add new comment
- 150 views