The SQL AVG() Function
The AVG() function returns the average value of a numeric column.
Example
Return the average price of all products:You can click on above box to edit the code and run again.
SELECT AVG(Price)
FROM products ;
Output
![avg price table](./images/avg price.png)
Note: NULL values are ignored.
Syntax
You can click on above box to edit the code and run again.SELECT AVG (column_name)
FROM table_name
WHERE condition;
Output
Demo Products table
This product table is used for examples:
![product table](./images/edited product tbl.png)
Add a Where Clause
You can add a WHERE clause to specify conditions:
Example
Return the average price of products in Product_id 2:You can click on above box to edit the code and run again.
SELECT AVG(Price)
FROM products
WHERE Product_id = 2 ;
Output
![avg price using where table](./images/avg price 2.png)
Use an Alias
It give the summarized column a name by using the AS keyword .
Example
Name the column "average price":You can click on above box to edit the code and run again.
SELECT AVG(Price) AS Average_price
FROM products ;
Output
![avg with AS keyword](./images/avg price 3.png)
Higher Than Average
To list all records with a higher price than average, we can use the AVG()function in a sub query: