The
AVG function is used to find out the average value of a specified column among a given set of records.
SQL AVG() SYNTAX
Select AVG (column_name) from tablename;
The
Products table has the following records:
Prd001 |
Pen |
40.25 |
Prd002 |
Pen |
100.75 |
Prd003 |
Eraser |
10.35 |
Prd004 |
Pencil |
20.45 |
Prd005 |
Book |
50.65 |
Prd006 |
Gum |
25.22 |
To calculate the average price of all the products, the 'avg' function is used on the
ProductPrice
column as shown below:
Select AVG (ProductPrice) from Products;
Since SQL Server returns the value with its own default column name, we can assign an alternate meaningful
name through the SQL command as shown below:
Select AVG (ProductPrice) as AvgPrice FROM Products;