MySQL AVG is one of the Aggregate Function, which is used to calculate the Average of total rows (or records) selected by the SELECT Statement. For example, If you want to find the Average Sales in your Area, or to find Average Cost of a laptops present in your Store then you can simply use the Avg function.
MySQL Avg Function Syntax
The basic syntax of the AVG in MySQL is as shown below:
SELECT AVG ([Column_Name]) FROM [Source]
In this article we will show you, How to find Average in MySQL with example. For this, We are going to use the below shown data
MySQL AVG Example
The Avg function in MySQL returns the Average of total records present in the specified column. For example, The following query will calculate the Average of total number records present in Yearly_Income column from customerdetails table.
-- AVG in MySQL Example SELECT AVG(Yearly_Income) AS `Average Income` FROM customerdetails;
MySQL AVG Function – Group By Example
In general, we use this Avg function to check for Average product price belongs to particular color or category etc. In this situation, we can use Group By Clause to group the products by color or category. Next, we use MySQL Average Function to find the average of each group. Let us see the Example
-- MySQL AVG Function Example USE company; SELECT Education, AVG(Yearly_Income) FROM customerdetails GROUP BY Education;
Above SQL Query will group the Customers by their education, and finds the average income of each group (education qualification)
MySQL AVG Function – Distinct Example
MySQL allows you to use the DISTINCT keyword along with the AVG function. AVG (DISTINCT Column_Name) calculate the average of the Unique number of records present in the table.
TIP: In MySQL, DISTINCT Keyword is used to remove the Duplicates from the specified column Name.
-- MySQL AVG Function Example USE company; SELECT Education, AVG(DISTINCT Yearly_Income) FROM customerdetails GROUP BY Education;
Above Query will find Unique number of records (by removing duplicates) present in Yearly_Income column, and then calculates the average of them
Thank You for Visiting Our Blog