SQL Server is a popular relational database management system that is widely used across industries. One of its most powerful functions is the AVG() function, which calculates the average value of a given column.

This article explores the syntax and usage of SQL Servers AVG() function, highlighting the key differences between the optional ALL and DISTINCT keywords. We also provide several examples to demonstrate how to use the AVG() function in various contexts.

SQL Server AVG() Function Syntax:

The syntax for the AVG() function is relatively simple. To calculate the average value of a column, you simply need to specify the name of the column as an argument within the AVG() function:

## SELECT AVG(column_name)

FROM table_name;

For instance, if you want to calculate the average salary of employees in a company, you would use the following query:

## SELECT AVG(salary)

FROM employees;

By default, the AVG() function uses the keyword DISTINCT. This means that it only calculates the average of distinct values rather than all data points.

If you want to include all data points in the calculation, you can specify the keyword ALL after the AVG() function, as shown below:

## SELECT AVG(ALL column_name)

FROM table_name;

Difference Between ALL and DISTINCT in AVG() Function:

The main difference between the ALL and DISTINCT keywords in the AVG() function is how they handle duplicate values. When using the DISTINCT keyword, the AVG() function only calculates the average of unique or distinct values.

In contrast, the ALL keyword calculates the average of all values, including duplicates. Let’s take a look at an example to illustrate this difference:

Suppose you have a table called sales, which contains the following data:

| Product | Sales |

|———|——-|

| A | 100 |

| B | 200 |

| C | 200 |

| D | 300 |

If you use the AVG() function with the DISTINCT keyword on the Sales column, the query will return the following result:

## SELECT AVG(DISTINCT Sales)

FROM sales;

Result: 200

As you can see, the AVG() function only considers the distinct values in the Sales column, which are 100, 200, and 300. The average of these values is 200.

Now, if you use the ALL keyword instead, the AVG() function will calculate the average for all rows, including duplicates:

## SELECT AVG(ALL Sales)

FROM sales;

Result: 200

In this case, AVG() considers all values in the Sales column, which are 100, 200, 200, and 300. The average of these values is still 200.

SQL Server AVG() Function Examples:

1. Simple Example of AVG() Function:

Let’s start with a simple example to illustrate the basic usage of the AVG() function.

Suppose you have a table called grades, which contains the following data:

| Student | Grade |

|———|——-|

| Alice | 80 |

| Bob | 90 |

| Charlie | 75 |

| Dave | 85 |

To calculate the average grade for the class, you would use the following query:

## SELECT AVG(Grade)

FROM grades;

Result: 82.5

As you can see, the AVG() function returns the average grade for the entire class, which is 82.5.

2. Example of AVG() Function with GROUP BY Clause:

Sometimes, you may want to calculate the average value of a column based on specific groups within the data.

In such cases, you can use the GROUP BY clause along with the AVG() function to generate a grouped average. Let’s add another column to our grades table to show the subject each student is taking:

| Student | Grade | Subject |

|———|——-|———|

| Alice | 80 | Math |

| Bob | 90 | Math |

| Charlie | 75 | English |

| Dave | 85 | English |

Now, let’s try to calculate the average grade for each subject using the GROUP BY clause:

SELECT Subject, AVG(Grade)

## FROM grades

GROUP BY Subject;

## Result:

| Subject | AVG(Grade) |

|———|————|

| Math | 85 |

| English | 80 |

In this example, the AVG() function calculates the average grade for each distinct subject. The GROUP BY clause is used to group the data by subject, thereby enabling the AVG() function to produce a separate average for each subject.

3. Example of AVG() Function in HAVING Clause:

The HAVING clause is used in conjunction with the GROUP BY clause to filter the results based on a condition.

Let’s use the grades table again to illustrate the usage of the HAVING clause with the AVG() function:

SELECT Subject, AVG(Grade)

## FROM grades

## GROUP BY Subject

HAVING AVG(Grade) > 80;

## Result:

| Subject | AVG(Grade) |

|———|————|

| Math | 85 |

In this query, the HAVING clause filters the results by subject whose average grade is greater than 80. As you can see, only Math satisfies this condition, producing an average grade of 85.

## Conclusion:

In conclusion, the AVG() function is a powerful and flexible tool that can be used to calculate the average value of a column in SQL Server. By using the optional ALL and DISTINCT keywords, you can customize the calculation to include or exclude duplicates.

Moreover, the AVG() function can be used in conjunction with other SQL keywords such as GROUP BY and HAVING to generate meaningful insights from your data. We hope that this article has provided you with a deep understanding of the syntax and usage of SQL Server AVG() function, and has equipped you with the knowledge to apply this function to your own data analysis use cases.

## An additional topic to cover in detail:

4. Example of AVG() Function with Subqueries:

Subqueries are queries that are nested within another query and are used to retrieve data from tables based on criteria specified in the outer query.

In several cases, it is necessary to create results based on a subquery, and AVG() function is widely used in subqueries, especially for calculations based on group data. In this scenario, the subquery is embedded into the main query, which then produces comprehensive results by joining the subquery with the main querys data.

Let’s take a look at an example:

Suppose you have two tables called (i) grades and (ii) students, and here is the structure of each table:

Table 1: grades

| Student ID | Course ID | Grade |

|————|———–|——-|

| 1 | 1 | 78 |

| 2 | 2 | 90 |

| 3 | 3 | 85 |

| 4 | 1 | 92 |

Table 2: students

| Student ID | Student_Name |

|————|————–|

| 1 | Alice |

| 2 | Bob |

| 3 | Charlie |

| 4 | Dave |

Now, let’s calculate the average grade for each student in a particular course. To do so, you can use a subquery as shown below:

SELECT students.Student_Name,

## (

## SELECT AVG(Grade)

## FROM grades

WHERE grades.Student_ID = students.Student_ID

AND grades.Course_ID = ‘1’) AS ‘Course_1_Avg’

FROM students;

## Result:

| Student_Name | Course_1_Avg |

|————–|————–|

| Alice | 85 |

| Bob | NULL |

| Charlie | NULL |

| Dave | 92 |

In the above example, the subquery plays a critical role.

It calculates the average grade for the specific course that each student has taken while also making sure to specify the specific course ID. The subquery also includes the WHERE clause that links the grades with students’ table.

Here, the grades table’s Student ID is compared with the students’ table’s student identifier. It only offers a specific Course ID that matches the course for which you want to calculate the average.

As you can see, the main query joins the grades table with the students’ table with the help of a subquery to produce accurate and detailed results. Conclusion:

In conclusion, SQL Server’s AVG() function is an incredibly useful tool that can help in a range of business applications.

Whether calculating the average value of a column, using DISTINCT or ALL keywords to exclude or include duplicates, using GROUP BY and HAVING to produce grouped data, or using subqueries with the AVG() function to calculate data from joined tables, AVG() function is versatile and powerful. It is essential to know its syntax and usage to derive meaningful insights from your data.

By following the examples provided in this article, you should be able to use SQL Server’s AVG() function confidently to calculate the averages for any data points that require them. In conclusion, SQL Server’s AVG() function is a powerful tool that can help make sense of data in various contexts.

By calculating the average value of a given column, users can generate significant insights into a dataset. With options such as DISTINCT, ALL, GROUP BY/HAVING clauses, and subqueries, the AVG() function is versatile and can be adapted to produce targeted and specific results.

Understanding the syntax and usage of this powerful function can empower SQL Server users to analyze data more effectively and generate valuable insights for their organizations. Thus, the article highlights the importance of the AVG() function and its role in sophisticated data analysis applications.