Tutorialsteacher

Follow Us

SQL - AVG() Function

The AVG() is an aggregate function that is used to find out an average of the list of values of columns or an expression.

The AVG() function is used with the numeric columns only.

Syntax:

SELECT AVG(column_name) FROM table_name WHERE condition;

For the demo purpose, we will use the following Employee table in all examples here.

EmpIdFirstNameLastNameEmailSalaryDeptId
1JohnKing'[email protected]'2400010
2JamesBond1700020
3NeenaKochhar'[email protected]'1500020
4LexDe Haan'[email protected]'900030
5AmitPatel6000030
6AbdulKalam'[email protected]'480040

The following query calculates the average salary of all the employees.

SQL Script: AVG()
SELECT AVG(Salary) AS AVERAGE FROM Employee;
AVERAGE
21633

You can also calculate the average salary of each department using the following command:

SQL Script: AVG()
SELECT DeptId, AVG(Salary) AS AVERAGE FROM Employees GROUP BY DeptId;
DeptIdAVERAGE
1024000
2016000
3034500
404800