Updated March 10, 2023
Introduction to SQL avg()
SQL avg() is one of the aggregate functions available in SQL that helps us to fetch the average value among multiple values that are specified in the column values of records, the expression consisting of the column that is mentioned. Most of the time, when a query is used to retrieve the data that report related and contains a group by statement the avg() function is used to get the average value of a particular column or columns based on the grouping function. In this, article, we will learn about the syntax and usage of SQL avg() function and then implement the function in various examples involving literal values, column values, column values with grouping statements, etc to get a clear idea about its usage.
Syntax and Usage
The syntax of NOT IN statement in SQL is as shown below –
SELECT AVG(expression)
FROM table_name
[WHERE restriction];
Where expression can be any name of the column of the table or a formula built up using column names and static literal values or variables. The table_name is the name of the table from which you want to retrieve the records and calculate the average value from one of their columns. The use of the FROM table name clause is required. One optional thing is the use of a where clause to mention the conditions and restrictions that should be fulfilled by the records of the table to consider that record’s column value for calculating the average value.
When where clause is used only filtered out data is considered for the average calculation by AVG() function. The AVG() function collects all the values of the expression mentioned in it and adds them up which is further divided by the number of values or expressions that were added to find out the final average value. For example, consider that we have to find out an average of 50, 100, 150, and 200. Then the average function will internally calculate 50 + 100 + 150 + 200 which evaluates to 500 and then divides the sum by 4 as 4 numbers were added that ultimately gives the average value 125.
Examples
We will study the implementation of the AVG() function in the following examples.
Examples #1 – Using a Single Column
Let us firstly consider a simple example that we used above to explain the working of the AVG() function. We will calculate the average value of SQL numbers using AVG() function. Let us create one simple table named numbers and store the num column value in it. We will use the following query statement to create our table –
CREATE TABLE numbers (num INT) ;
The execution of the above query statement will give the following output –
Now, we will insert the above records in the table. Our query statement will be as follows –
INSERT INTO numbers(num) VALUES (50), (100), (150), (200);
The execution of the above query statement will give the following output –
Let us now retrieve the records once –
SELECT * FROM numbers ;
The execution of the above query statement will give the following output –
Now, we will calculate the average of num column of numbers table using AVG() function using the following query statement –
SELECT AVG(num) FROM numbers ;
The execution of the above query statement will give the following output –
Example #2 – Using Distinct Function
We can use the distinct function in AVG() function to consider the repetitive values of the column of the table only once while calculating the average value. Suppose that, we insert some more records in numbers table using the following query statement –
INSERT INTO numbers(num) VALUES (350), (800), (150), (300),(450), (100), (250);
The execution of the above query statement will give the following output –
If we use, SELECT AVG(num) FROM numbers; statement to calculate the average value of num column then each of the values will be considered while calculating the average value. If we use the following statement –
SELECT AVG(DISTINCT(num)) FROM numbers ;
The output will be different than that of the first query without distinct function because internally the calculation of average value by AVG() function will only consider the column values that are repeated such as 100 and 150 only once. The output of both of the above queries is as shown below-
Example #3 – Using Formula
We can use the expressions in the AVG() function to consider the value evaluated by calculating each of the formula or expression values containing column value to calculate the average value. Let us consider one example, we will calculate the average of all the columns after they are multiplied by 10 and added by 1. Our query statement will be as follows –
SELECT AVG((num * 10) + 1) FROM numbers ;
The execution of the above query statement will give the following output –
We can even use the existing functions such as SUM() and COUNT() inside the AVG() function.
Example #4 – Using Group by
When we have complex tables and relations between multiple tables, we have to query on those tables using joins to retrieve data usually for reporting purpose that consists of summarized data. Even in some scenarios, the data from a single table need to be manipulated to get summarized data. Suppose that we have one table named workers consisting of following records in it that are retrieved by executing a simple select query on that table –
SELECT * FROM workers;
The execution of the above query statement will give the following output –
Now, the situation is such that we have to calculate the average salary of the workers per team. The output should consist of the team id and the average salary of that team. For this, we will have to use the group by statement and group the records based on team id, and calculate the average salary by using AVG() function.
Our query statement will be as follows –
SELECT
team_id,
AVG(salary)
FROM
workers
GROUP BY team_id ;
The execution of the above query statement will give the following output along with team ids and their respective average salaries –
Conclusion – SQL avg()
We can use the AVG() function in SQL to calculate the average value of the columns of the tables or average of expressions that involve column values and even calculate the average value of columns in the grouped manner using GROUP BY statement.
Recommended Articles
We hope that this EDUCBA information on “SQL avg()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.