MySQL AVG() function

5 (100%) 2 votes

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_nameFROM table_name WHERE condition;

SELECT AVG(distinct column_nameFROM 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:

For any query or issue, feel free to discuss on http://discuss.eduguru.in
%d bloggers like this: