Table of Contents
Understanding the AVG() Aggregate Function in SQL: A Complete Guide
The AVG() function returns the average value of a numeric column.
The SUM() function returns the total sum of a numeric column.
Syntax: AVG()
SELECT AVG(column_name) FROM table_name WHERE condition;
We will apply AVG function in this below table:
We will find the AVG employee salary
|
EmpId |
EmpName |
EmpAddress |
EmpSalary |
EmpDept |
|
11 |
Rambo |
Kolkata |
30000 |
ADMIN |
|
21 |
Inza |
Bihar |
31000 |
SALES |
|
32 |
Samser |
Kolkata |
32000 |
IT |
|
41 |
Kamran |
Hydrabad |
33000 |
ITIS |
|
52 |
Azam |
Kolkata |
33000 |
ITIS |
Example: COUNT
Code:
SELECT AVG(EmpSalary) AS AvgSalary FROM EmployeeDetails
You have to remember EmpSalary is a numeric column
Output:
The above code will produce the following result-
AvgSalary
31800
Code: Required Code to create the table
USE SQLExamples
DROP TABLE EmployeeDetails
CREATE TABLE EmployeeDetails(
EmpId int,
EmpName VARCHAR(30),
EmpAddress VARCHAR(50),
EmpSalary int,
EmpDept VARCHAR(10)
)
INSERT INTO EmployeeDetails VALUES
(11, 'Rambo', 'Kolkata', 30000, 'ADMIN'),
(21, 'Inza', 'Bihar', 31000, 'SALES'),
(32, 'Samser', 'Kolkata', 32000, 'IT'),
(41, 'Kamran', 'Hydrabad', 33000, 'ITIS'),
(52, 'Azam', 'Kolkata', 33000, 'ITIS')
SELECT * FROM EmployeeDetails