SQL AVG Function

The SQL AVG() function is an aggregate function used to calculate the average value of a numeric column in a result set. It helps in obtaining the average of values from a column or those meeting specific conditions.

AVG() Function

The AVG() function is used in queries to find the average value of a numeric column.


-- Calculate the average salary of all employees
SELECT AVG(Salary) AS AverageSalary
FROM Employees;

-- Calculate the average score of students in a specific course
SELECT AVG(Score) AS AverageScore
FROM StudentScores
WHERE CourseID = 5;

        

Syntax

The basic syntax for using the AVG() function is:


SELECT AVG(column_name) AS Alias
FROM table_name
WHERE condition;

        

Examples

Here are some examples of using the AVG() function:


-- Calculate the average price of products
SELECT AVG(ProductPrice) AS AveragePrice
FROM Products;

-- Calculate the average duration of customer calls
SELECT AVG(CallDuration) AS AverageCallDuration
FROM CustomerCalls
WHERE CallDate BETWEEN '2024-01-01' AND '2024-06-30';