Quick Reach
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.
See an example of AVG function
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:
1
|
select avg(emp_sal_paid) as Avg_salary from tbl_emp_salary_paid
|
See this example online with graphic
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.
The query:
1
2
3
|
select emp_name,avg(emp_sal_paid)as Avg_paid from tbl_emp_salary_paid
group by emp_name
|
See this example online
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.
Also see – SQL count function | The Min function | Max function
Leave A Comment?