AVG function in SQL

The AVG function returns an average value, ignoring null values of a set of numbers. So, this function is used with a particular numeric column.

SQL Syntax

Select AVG(column_name) from table_name WHERE condition;

Example: Create an Employees table.

SQL

CREATE TABLE Employees (
   EmployeeID INT,
    FirstName VARCHAR(20),
    LastName VARCHAR(20),
    Age int
); 

Insert the data in the Employees table.

SQL

insert into Employees values
(1, "Ashish", “Goel", 25), 
(2, "Kirshan", "Gupta", 30),
(3, "Anjali", "Sharma", 25),
(4, "Manju", "Saini", 15),
(5, "Katrina", "Kaif", 18),
(6, "Esha", "Dixit", 20),
(7, "Ankita", "Verma", 19), 
(8, "Meenakshi", "Chikkara", 35),
(10, "Alia", "Bhatt", 16);  

By using the Create table and insert statement we reach to the following table in our database:

Table: Employees

EmployeeIDFirst NameLast NameAge
1AshishGoel25
2KirshanGupta30
3AnjaliSharma25
4ManjuSaini15
5KatrinaKaif18
6EshaDixit20
7AnkitaVerma19
8MeenakshiChikkara35
10AliaBhatt16

Use the AVG() function to calculate the average age of the employees.

SQL

SELECT 
    avg(AGE) as AverageAge
FROM Employees;  

The output of the above query is shown below:

AverageAge
22.5556