AVG function in MySQL

MySQL AVG
In MySQL, the AVG function is used to get the average value of an expression.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT AVG (aggregate_expression)
FROM table_name
WHERE conditions;
SELECT AVG (aggregate_expression) FROM table_name WHERE conditions;
SELECT AVG (aggregate_expression)  
FROM table_name  
WHERE conditions;

Parameters:

Aggregate_expression: It is used to specify the column or expression to be utilized by the AVG function.

Example: Items table:

ID NAME QUANTITY
1 Electronics 30
2 Sports 45
3 Fashion 100
4 Grocery 90
5 Toys 50

 

Query:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT AVG(quantity) AS “Average Quantity”
FROM items;
SELECT AVG(quantity) AS “Average Quantity” FROM items;
SELECT AVG(quantity) AS “Average Quantity”
FROM items;

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
AVERAGE QUANTITY
63
AVERAGE QUANTITY 63
AVERAGE QUANTITY
63

Explanation:

The ‘items’ is an already existing table from which we are calculating the average value of ‘quantity’.