SQL Server AVG() Function
The AVG()
function in SQL Server calculates the average (mean) of a set of numeric values. It's a fundamental aggregate function, used to summarize numerical data.
AVG(): Definition and Usage
AVG()
is incredibly useful for getting a quick overview of the central tendency of your data. It automatically ignores any NULL
values, giving you a clean average of only the non-null numeric values in the specified column or expression.
Syntax
Syntax
AVG(expression)
Parameter Values
Parameter | Description |
---|---|
expression |
A numeric column name or a formula that evaluates to a number. This is required. |
Examples
Calculating the Average Price
This example calculates the average price of all products in the 'Products' table (assuming it has a 'Price' column).
Syntax
SELECT AVG(Price) AS AveragePrice FROM Products;
Output
AveragePrice
------------
17.67 (Example output - this will vary depending on the data in your Products table)
Selecting Products Above Average Price
This example shows how to use AVG()
in a subquery to select products with a price above the average price.
Syntax
SELECT * FROM Products WHERE Price > (SELECT AVG(Price) FROM Products);
Output
ProductID | ProductName | Price
------------------------------------------
(Products with a price above the average will be listed here)