AVG Function - ( SQL Server Aggregate Function )
The AVG function is a SQL Server aggregate function used to calculate the average of a set of values. It can be used with numeric data types such as INT, BIGINT, DECIMAL, FLOAT, etc. In this page, we will discuss the syntax, example, output, explanation, use, important points, and summary of the AVG function in SQL Server.
Syntax
The syntax of the AVG function is:
SELECT AVG(column_name)
FROM table_name
Here, column_name is the name of the column or expression you want to calculate the average for, and table_name is the name of the table containing the column data.
Example
Consider a table named "Orders" containing the following data:
OrderID | ProductID | Quantity | Price |
---|---|---|---|
1 | 1 | 10 | 50.0 |
2 | 2 | 20 | 25.0 |
3 | 1 | 15 | 100.0 |
4 | 3 | 5 | 75.0 |
5 | 2 | 30 | 50.0 |
To calculate the average price of all orders, we can use the following SQL query:
SELECT AVG(Price) AS AvgPrice
FROM Orders
The output of this query will be:
AvgPrice |
---|
60.0 |
Output
The AVG function returns a single value which represents the average of the values in the specified column.
Explanation
The AVG function works by taking a set of values and summing them, then dividing by the total number of values. It is used to calculate the average of a set of values such as prices, quantities, or any other numeric data type.
Use
The AVG function is used to calculate the average of a set of values in a column. It is useful for calculating averages for data analysis, or for summarizing data in reports.
Important Points
- The AVG function is a SQL Server aggregate function used to calculate the average of a set of values.
- It can be used with numeric data types such as INT, BIGINT, DECIMAL, FLOAT, etc.
- The AVG function returns a single value which represents the average of the values in the specified column.
Summary
In this page, we discussed the AVG function in SQL Server. We talked about the syntax, example, output, explanation, use, important points, and summary of the AVG function. The AVG function is a useful tool for calculating averages for data analysis or summarizing data in reports.