Syntax of using the Avg function
The general syntax of using SQL average function is:
Select avg(numeric_column_name) from table_name
Where col_name = value
The Avg function of SQL
- The Avg() returns the average value of the given numeric column.
- You can use the Avg function in the select statement.
- You can also use it with the group by clause.
An Avg function example
To understand the Avg function, we are going to use the same table that we used for other aggregate functions of SQL i.e. tbl_emp_salary_paid. This table is used to store employees salaries that are paid every month (for illustration purpose). You can see it by clicking the demo link with each example below.
Example of using SQL Average in simple select statement
This example calculates and returns the Average salary paid in the tbl_emp_salary_paid table.
The SQL query with AVG:
select avg(emp_sal_paid) as Avg_salary from tbl_emp_salary_paid
Group by example with Avg function
This query will return average salary paid to each employee by using the SQL Avg function with the group by clause.
select emp_name,avg(emp_sal_paid)as Avg_paid from tbl_emp_salary_paid
group by emp_name
As you can see in the above graphic, two queries are shown. One with the simple select statement to show the complete table data. The other query uses the group by clause with the Avg function.