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
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:
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
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
Higher Than Average
To list all records with a higher price than average, we can use the AVG()function in a sub query: