MariaDB AVG() Function

MariaDB AVG() function is used to retrieve the average value of an expression.

Syntax:


SELECT AVG(aggregate_expression)
FROM tables
[WHERE conditions]; 

Or


SELECT expression1, expression2, ... expression_n,
AVG(aggregate_expression)
FROM tables
[WHERE conditions]
GROUP BY expression1, expression2, ... expression_n; 

Example:


# AVG() Function with single expression

We have a table "Employee2", having the following data:

MariaDB Avg function 1

Let's retrieve the average salary of the employees from the table.


SELECT AVG(salary) AS "Average Salary"
FROM Employee2;

Output:

MariaDB Avg function 2

# Use AVG() Function with Formula

You can use AVG() function also with mathematical formula. For example, if you want to increase the average salary of your employees by 50%, you can use the following formula to estimate the cost:


SELECT AVG(salary * 1.5) AS "New Average"
FROM Employee2; 

Output:

MariaDB Avg function 3

It will increase the salary by 50%.


# AVG() Salary with ORDER BY Clause


SELECT emp_id, AVG(salary) AS "Average Salary"
FROM Employee2
GROUP BY emp_id; 

Output:

MariaDB Avg function 4