How to Use the AVG() Function in MySQL

08/04/2021

Contents

In this article, you will learn how to use the AVG() function in MySQL.

Using the AVG() function in MySQL

The AVG() function in MySQL is used to find the average value of a column in a table.

Syntax

The syntax for using the AVG() function in MySQL is as follows:

SELECT AVG(column_name) FROM table_name;

Examples

Suppose we have a table named “employees” with columns “employee_id” and “salary”. We can use the AVG() function to find the average salary in the table as follows:

SELECT AVG(salary) FROM employees;

This query will return the average value of the “salary” column in the “employees” table.

If we want to find the employees with salaries greater than the average, we can use the following query:

SELECT employee_id, salary FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);

This query will return the employee ID and salary of the employees with salaries greater than the average salary in the “employees” table.

We can also use the AVG() function with multiple columns to find the average value of multiple columns. For example:

SELECT AVG(column1), AVG(column2) FROM table_name;

This query will return the average value of “column1” and “column2” in the “table_name” table.