TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

MySQL average()

MySQL average() function example with examples on CRUD, insert statement, select statement, update statement, delete statement, use database, keys, joins etc.

<< Back to MYSQL

MySQL avg() function

The MySQL avg() is an aggregate function used to return the average value of an expression in various records.

Syntax

The following are the basic syntax an avg() function in MySQL:

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

Parameter explanation

aggregate_expression: It specifies the column or expression that we are going to find the average result.

table_name: It specifies the tables from where we want to retrieve records. There must be at least one table listed in the FROM clause.

WHERE conditions: It is optional. It specifies the conditions that must be fulfilled for the records to be selected.

MySQL avg() function example

Consider our database has a table named employees, having the following data. Now, we are going to understand this function with various examples:

mysql average

1. Basic Example

Execute the following query that calculates the average working hours of all employees in the table:

mysql> SELECT AVG(working_hours) Avg_working_hours FROM employees;

Output:

We will get the result as below:

mysql average

2. MySQL AVG() function with WHERE clause

The WHERE clause specifies the conditions that must be fulfilled for the selected records. Execute the following query to calculate the total average working hours of employees whose working_hours >= 12.

mysql> SELECT AVG(working_hours) Avg_working_hours FROM employees WHERE working_hours>=12;

Output:

It will give the following output:

mysql average

3. MySQL AVG() function with GROUP BY clause

The GROUP BY clause is used to return the result for each group by one or more columns. For example, this statement calculates the average working hours of each employee using the AVG() function and then group the result with the GROUP BY clause:

mysql> SELECT emp_name, occupation, AVG(working_hours) Avg_working_hours FROM employees GROUP BY occupation;

Output:

Here, we can see that the total working hours of each employee calculates by grouping them based on their occupation.

mysql average

4. MySQL AVG() function with HAVING clause

The HAVING clause is used to filter the average values of the groups in MySQL. Execute the following statement that calculates the average working hours of all employees, grouping them based on their occupation and returns the result whose Avg_working_hours>9.

mysql> SELECT emp_name, occupation, 
AVG(working_hours) Avg_working_hours 
FROM employees 
GROUP BY occupation 
HAVING AVG(working_hours)>9;

Output:

mysql average

5. MySQL AVG() function with DISTINCT clause

MySQL uses the DISTINCT keyword to remove the duplicate rows from the column name. This clause is used with this avg() function to return the average value of a unique number of records present in the table.

Execute the following query that removes the duplicate records in the working_hours column of the employee table and then returns the average value:

mysql> SELECT emp_name, occupation, 
AVG(DISTINCT working_hours) Avg_working_hours
FROM employees
GROUP BY occupation;

Output:

mysql average
Next TopicMySQL min()




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf