Site icon Database Tutorials

AVG Function in SQL Server(TSQL)

 

We use the AVG function to obtain the average value of the specified column. Its one of the most used SQL Server functions.

Let’s make examples for a better understanding of AVG Function.

Example:

First, we create a table with the help of the following script and add a few records into this table.

Let’s calculate the average prices of all products in the ProductTable table using the AVG function:

AVG Function with GROUP BY Clause:

We can also use the AVG function with GROUP BY Clause. For more information about GROUP BY Clause, I suggest you read the article “Group By Clause, SUM, MAX, MIN, AVG, COUNT and COUNT_BIG Functions in SQL Server“.

Let’s group the data by ProductCategory and calculate the average prices of the groups with GROUP BY Clause:

AVG Function with OVER Clause:

You can find details in the article “OVER Clause in SQL Server (TSQL)“.

Exit mobile version