MySQL AVG Function


Demo Database:
  • Table Name : products
product_id product_name product_code price unit category_id
1 The Psychology 7550 250.00 Piece 1
2 Stories for Children 3098 350.00 Piece 1
3 Harry Potter 8472 275.00 Piece 1
4 Tecno Spark 9468 8000.00 Nos 2
5 Samsung Galaxy 7188 10000.00 Nos 2
6 Panasonic Eluga 3433 7000.00 Nos 2
7 Lenovo IdeaPad 6708 45000.00 Nos 3
8 ASUS Celeron Dual Core 3583 43000.00 Nos 3

1.Avg()

MySQL AVG() function returns the average value of a numeric column.

Example

Returns the average price of all products.

SELECT Avg(price) FROM products;
Try it Yourself

2.Avg() Function with Where Clause

The MySQL AVG() function can be used with different functions and clauses such as the WHERE clause and the GROUP BY clause.

Example

Returns the average price in the product table using where clause.

SELECT Avg(price) FROM products where unit='NOS';
Try it Yourself