You are reading the article **Implementation Of Sql Avg() Function** updated in September 2023 on the website Saigonspaclinic.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. *Suggested October 2023 Implementation Of Sql Avg() Function*

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax and UsageThe syntax of NOT IN statement in SQL is as shown below –

SELECT AVG(expression) FROM table_name [WHERE restriction];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.

ExamplesWe will study the implementation of the AVG() function in the following examples.

Examples #1 – Using a Single ColumnLet 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 FunctionWe 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 FormulaWe 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 byWhen 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 ArticlesWe hope that this EDUCBA information on “SQL avg()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

You're reading __Implementation Of Sql Avg() Function__

Update the detailed information about **Implementation Of Sql Avg() Function** on the Saigonspaclinic.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!