Updated May 10, 2023
Introduction to AVG() in MySQL
Among many kinds of SQL languages, we use an aggregate function in Data Query Language (DQL) with a SELECT statement. This function acts on multiple values or records and returns a single value. The single value may be of sum, average, minimum, or maximum among all the values. In this topic, we will learn about MySQL avg().
Example: if we have records of all the citizens of India, both district and state-wise, forgetting the total strength of India, we need to use the aggregate function like SUM(). In the same manner, if we want to know the highest, lowest, or average population, either state or district-wise, we have to use functions like MAX(), MIN(), and AVG(), respectively. But this aggregate function does not act on null values except functions like COUNT(). There is another function called GROUP BY(); in many scenarios, aggregate functions are used along with this.
SELECT avg(sales) FROM product;
SELECT * FROM product GROUP BY product_name HAVING avg(sales) > 1000;
How does avg() work in MySQL?
Now, let us discuss how avg() works in MySQL?
Above is the student table database. This table has three attributes: section, name, and marks. With the help of the aggregate function avg(), we will perform some examples for a better understanding of this function.
To get average marks from all the students:
SELECT AVG(marks) AS avg_marks FROM student;
If we want the average mark of students for each section, we can use AVG() with the GROUP BY function.
SELECT section, AVG(marks) AS avg_marks FROM student GROUP BY section;
Examples to Implement avg() in MySQL
Let’s create another employee table with the following attributes. Deep-diving into this table can give us depth knowledge about avg() aggregate function.
It will show the average salary of all employees
SELECT avg(salary) AS average_salary FROM employee;
It will show the average salary according to each manager’s id
SELECT manager_id, avg(salary) FROM employee GROUP BY manager_id;
It will show the average salary for each manager id whose average salary will be greater than 11000
SELECT manager_id, avg(salary) FROM employee GROUP BY manager_id HAVING avg(salary) > 11000;
It will show the average salary according to each department’s id
SELECT department_id, avg(salary) FROM employee GROUP BY department_id;
It will show the average salary according to each department id where the average salary will be greater than 11500
SELECT department_id, avg(salary) FROM employee GROUP BY department_id HAVING avg(salary)>11500;
It will show the average salary according to each job id
SELECT job_id, avg(salary) FROM employee GROUP BY job_id;
It will show the average salary according to each job id where the average salary will be greater than 12000
SELECT job_id, avg(salary) FROM employee GROUP BY job_id HAVING avg(salary)>12000;
From an analysis perspective and in the extraction process from the database, those aggregated functions are essential as it helps us bring down complex scenarios. Specifically, aggregated functions play major roles when we give conditions in the query or while using the Windows function. Some functions like LIMIT, RANK, etc., and GROUP BY clause always comes with aggregated functions. While working on huge data like supply chains, we need to use these aggregated functions always as it simplifies our data. The same happens using Tableau or Power BI; it always aggregates those values for better visualization.
We hope that this EDUCBA information on “MySQL avg()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.