Open In App

AVG() Function in SQL Server

Last Updated : 11 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The AVG() function in SQL Server is an essential aggregate function used to compute the average value of a numeric column.

It works by summing all non-NULL values in the specified column and then dividing the total by the number of these values. In this article, We will learn about AVG() Function in SQL Server by understanding various examples in detail.

AVG() Function in SQL Server

  • The AVG() function in SQL Server is an aggregate function used to calculate the average value of a numeric column.
  • It sums up all the values in the column and divides the total by the number of non-NULL values.
  • The AVG() function ignores NULL values.

Features:

  • This function comes under Numeric Functions.
  • This function accepts only one parameter, namely expression.
  • This function ignores NULL values.

Syntax:

AVG(expression)

Parameter:

This method accepts one parameter.

  • expression – A specified numeric value may be either a stated field or a stated formula.

Returns:

It returns the average value of the specified expression.

Examples of AVG() Function in SQL Server

For better understanding of AVG() Function in SQL Server we will use the table called Sales which is shown below:

SaleID Product Quantity Price
1 Laptop 2 1000
2 Smartphone 5 500
3 Tablet 3 700
4 Laptop 1 1100
5 Smartphone 4 450
6 Laptop 2 NULL

Example 1: Basic SQL Server AVG() Function

Calculate the average price of all products in the Sales table, including non-NULL values only.

Query:

SELECT AVG(Price) AS AveragePrice
FROM Sales;

Output:

AveragePrice
750.00

Explanation:

This query calculates the average price of products in the Sales table. It ignores the row where the Price is NULL, averaging the prices 1000, 500, 700, 1100, and 450.

Example 2: Using SQL Server AVG() with GROUP BY

Calculate the average price for each product in the Sales table.

Query:

SELECT Product, AVG(Price) AS AveragePrice
FROM Sales
GROUP BY Product;

Output:

Product AveragePrice
Laptop 1050.00
Smartphone 475.00
Tablet 700.00

Explanation:

This query groups the data by product type and calculates the average price for each group.

Example 3: Using SQL Server AVG() in HAVING Clause

Find the products with an average price greater than 500.

Query:

SELECT Product, AVG(Price) AS AveragePrice
FROM Sales
GROUP BY Product
HAVING AVG(Price) > 500;

Output:

Product AveragePrice
Laptop 1050.00
Tablet 700.00

Explanation:

This query uses the HAVING clause to filter groups where the average price is greater than 500.

Example 4: AVG() Function with WHERE Clause

Calculate the average price of products where the quantity is greater than 3.

Query:

SELECT AVG(Price) AS AveragePrice
FROM Sales
WHERE Quantity > 3;

Output:

AveragePrice
475.00

Explanation:

This query calculates the average price of products where the Quantity is greater than 3. Only rows with quantities of 4 and 5 are considered.

Example 5: AVG() Function with ORDER BY Clause

Display the products and their average price, ordered by the average price in ascending order.

Query:

SELECT Product, AVG(Price) AS AveragePrice
FROM Sales
GROUP BY Product
ORDER BY AVG(Price);

Output:

Product AveragePrice
Smartphone 475.00
Tablet 700.00
Laptop 1050.00

Explanation:

This query groups by product and orders the results by average price in ascending order.

Example 6: AVG() Function with DISTINCT Clause

Calculate the average price of distinct prices in the Sales table.

Query:

SELECT AVG(DISTINCT Price) AS AveragePrice
FROM Sales;

Output:

AveragePrice
750.00

Explanation:

This query calculates the average of distinct price values. In this case, only unique prices (1000, 500, 700, 1100, and 450) are considered for the average.

Conclusion

The AVG() function in SQL Server provides a straightforward method for calculating averages, whether you are analyzing individual columns or grouped data. By utilizing additional clauses like GROUP BY, HAVING, WHERE, and ORDER BY, you can perform more detailed and specific analyses. Understanding and leveraging the AVG() function can significantly enhance data reporting and decision-making processes.



Next Article
Article Tags :

Similar Reads