How to use SQL AVG function to get average

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

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:


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:


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

Was this article helpful?

Related Articles

Leave A Comment?