MySQL AVG() function
- AVG function to calculate the average value of a set of values or an expression.
- The AVG() function returns the average value of a numeric column.
- If the function does not find a matching row, it returns NULL
- The DISTINCT option can also be used to return the average of the distinct values of expr.
SELECT AVG(column_name) FROM table_name WHERE condition;
SELECT AVG(distinct column_name) FROM table_name WHERE condition;
Example1:
mysql> SELECT AVG(no_page) FROM book_mast; +--------------+ | AVG(no_page) | +--------------+ | 286.6250 | +--------------+ 1 row in set (0.02 sec)
Example: 2
To calculate average buy price of all products in the products table, you use the AVG() function as shown in the following query:
|
1
2
3
4
|
SELECT
AVG(buyprice) ‘Average Price’
FROM
products;
|
1 thought on “MySQL AVG() function”
Comments are closed.