×

MySQL AVG function

This function works on numerical data type values. The average function shows the average value of the data set. If an average function returns a null value, then the row is not available in the table. The average value supports the arithmetic operation of the column values. The average function returns integer and non-integer values.

Syntax

The AVG () function syntax executes below.

AVG(function_expression)

The following syntax shows MySQL AVG () function with table data.

SELECT AVG(function_expression) FROM Table_Name;

The given syntax shows MySQL AVG () function with clause and condition.

SELECT AVG(function_expression) FROM Table_Name WHERE condition;

Description

  • The AVG() function returns average values of the table data or column data.
  • The "function_expression" represents the value or column of the table for function operation.
  • The "WHERE" clause is used to filter the records of the table.

Prerequisite

  • Create a table with the below statement.

mysql> create table crafts (  

    id int NOT NULL,

    product_name varchar(100),    

    quantity int,  

    date date

    );  

  • Insert the data in the table as per data type.

mysql> insert into crafts(id,

product_name, quantity,  

date) 

VALUES           

(1, 'pen', 80, '2021-08-18'),  

(2, 'sketch book', 65, '2021-07-11'),  

(3, 'stickers', 84, '2021-01-22'),  

(4, 'colors', 55, '2021-02-21'),  

(5, 'eraser', 96, '2021-06-22'),  

(6, 'sketch pencil', 98, '2021-01-15');

  • You can refer to the table structure and information using the output query.
mysql> SELECT * FROM crafts;
AVG() / average() function

Examples of the AVG function

1) Example: This example explains the basic AVG function. It takes a single column which is the "quantity" column of the table, and then finds its average value.

mysql> SELECT AVG(quantity ) AS quantity FROM crafts;

 OUTPUT    

AVG() / average() function

The average function returns the average value of the quantity column as output. The quantity column has approximately 80 average values, but the table shows floating values.

2) Example: This example uses the "DISTINCT" clause with the AVG function. This clause removes duplicate data and then displays the average values of these data.

mysql> SELECT AVG( DISTINCT quantity ) AS quantity FROM crafts;

 Output

AVG() / average() function

The AVG() function contains a quality column with the DISTINCT keyword. This keyword removes similar values in the column and returns an average value.

3) Example: The AVG function using the date value example shows below. This function returns the date value into an integer value. The example returns the average value of the entire date column.

mysql> SELECT AVG(date) AS date FROM crafts;

OUTPUT

AVG() / average() function

The average function supports the date and floating values of the table. The date converts into a number and returns the average date of the crafts table. You can see the date returns into floating value.

4) Example: The AVG function with the "DISTINCT" and the "WHERE" clauses example shows below. This example uses the average function on the quantity column. It returns the average "quantity" that has greater than 65 values in the table.

mysql> SELECT AVG(DISTINCT quantity ) AS quantity 
FROM crafts 
WHERE quantity  > 65;

 OUTPUT

AVG() / average() function

5) Example: The AVG function using multiple columns example shows below. This function operates on the "id", "date", and "quantity" columns of the crafts table and returns the average value of all columns.

mysql> SELECT AVG(id) AS id, 
AVG(date) AS date, 
AVG(quantity ) AS quantity 
FROM crafts;

OUTPUT

AVG() / average() function

The average function uses with the three columns of the crafts table. Each column required the "AVG()" method separately.

6) Example: The AVG function with the "WHERE" clause and "AND" operator example shows below. This function can operate arithmetic operation on several columns simultaneously. The "where" clause uses "greater than" and "less than" conditions.  The "AND" operator works on the "quantity" and "id" columns.

mysql> SELECT AVG(id) AS id, 
AVG(date) AS date, 
AVG(quantity ) AS quantity 
FROM crafts 
WHERE quantity  > 80 AND id < 4;

Output

AVG() / average() function

The output image shows one row of the average values of the table.

7) Example: The AVG function with the "GROUP BY" clause example shows below. This average function example uses the "GROUP BY" clause on the "quantity" column.

mysql> SELECT AVG(id) AS id, 
AVG(date) AS date, 
AVG(quantity ) AS quantity 
FROM crafts 
GROUP BY quantity;

OUTPUT

AVG() / average() function

This output display entire rows of the given table because of the "GROUP BY" clause. The id, date, and quantity columns display values with floating-point.

8) Example: The AVG function with the "WHERE" clause and "OR" operator example shows below. Here the WHERE clause applies two conditions using the "OR" logical operator.

mysql> SELECT AVG(id) AS id, 
AVG(date) AS date, 
AVG(quantity ) AS quantity 
FROM crafts 	
WHERE quantity > 80 OR id < 3;

OUTPUT

AVG() / average() function

Here, the image shows the single row of the average values using the "WHERE" condition. The quantity column applies to the "greater than" 80 value. The "id" column applies to the "less than" three values.

9) Example: The AVG function with the "GROUP BY" and "HAVING" clause example shows below. In this example, the "quantity" column modifies with the "GROUP BY" clause and "HAVING" condition. The "HAVING" clause applies the "greater than" condition.

mysql> SELECT AVG(id) AS id, 
AVG(date) AS date, 
AVG(quantity ) AS quantity 
FROM crafts 
GROUP BY quantity 
HAVING quantity > 85;

OUTPUT

AVG() / average() function

The output image shows two rows of the given columns. The table displays data greater than the "85" quantity of the crafts table.

10) Example: The AVG function with the multiple clauses example shows below.

The average function can be used in multiple columns of the table. The output returns particular table columns and rows.

mysql> SELECT AVG(id) AS id, 
AVG(date) AS date, 
AVG(quantity ) AS quantity 
FROM crafts 
WHERE id > 2 
GROUP BY quantity 
HAVING quantity > 85 
ORDER BY quantity ASC;

OUTPUT

AVG() / average() function

The average function shows output based on the WHERE and HAVING clauses. The WHERE clause uses the "greater than two rows" on the "id" column. The "HAVING" clause uses the "greater than 85 quantity" on the "quantity" column. The output displays two rows and three columns of the table data.


Related Topics

MySQL CROSS JOIN

MySQL CROSS JOIN combines all possibilities of the two or more tables and returns the result that contains every row from all contributing tables. It links several columns from the...

4 minutes read.

MySQL MOD() function

In this context, we will learn how we can use the MySQL MOD() function with proper syntax and good examples. Introduction of MySQL MOD() function Basically, in MySQL, the MOD() function is...

3 minutes read.

MySQL DATE_ADD() function

In this context, we will learn how we can use the MySQL DATE_ADD() function with proper syntax and good examples. Introduction of MySQL DATE_ADD() function In MySQL, the DATE_ADD() function is used...

3 minutes read.

MySQL Show index

This query helps to access and retrieve table information. This syntax displays the required index or all indexes of the table. It also displays the index type as per the...

3 minutes read.

MySQL MAX function

The MAX function is a type of aggregation function that determines the maximum value of the table data. This function works on numerical data type values. If the table displays...

4 minutes read.

MySQL DATE_FORMAT() function

In this context, we will learn how we can use the MySQL DATE_FORMAT() function with proper syntax and good examples. Introduction of MySQL DATE_FORMAT() function DATE_FORMAT() function in MySQL is used to...

3 minutes read.

MySQL LOG10() function

In this context, we will learn how we can use the MySQL LOG10() function with proper syntax and good examples. Introduction of MySQL LOG10() function To evaluate the natural logarithmic value of...

2 minutes read.

MySQL Invisible Index

Invisible Index MySQL index decides an option to the availability of the index for the query optimizer. This index shows the visible and invisible options to displays the index column in...

3 minutes read.

MySQL Operators

MySQL Operators MySQL operator needs advanced operation on the table and its data. This operator works with the "WHERE" clause. MySQL operators are a statement to modify information. It helps to...

13 minutes read.

MySQL EQUI JOIN

An equijoin is an operation that combines multiple tables based on equality or matching column values in the associated tables. This operation links more than two tables based on a...

5 minutes read.

MySQL GREATEST function

This statement displays the greatest values of the table. The GREATEST function returns a null value when the table contains a null value. The GREATEST function needs a minimum of...

2 minutes read.

MySQL Stored Procedure

MySQL creates the "stored procedure" function to operate database information. You can use parameters, blocks, and statements to create a new procedure. The procedure requires a database table to use...

3 minutes read.

MySQL MONTHNAME() function

In this context, we will learn how we can use the MySQL MONTHNAME() Function with proper syntax and good examples. Introduction of MySQL MONTHNAME() function In MySQL the MONTHNAME() Function is used...

3 minutes read.

MySQL: Entity-Relationship Model

Entity-Relationship Model Entity-Relationship model or E R model is used to create a relationship between different attributes or entities. It describes the structure of the database with the help of the...

5 minutes read.

MySQL Subquery

The subquery is a MySQL query used to manage data operations. Mainly subquery helps to retrieve data with the necessary condition. It creates a nested query with two different queries....

5 minutes read.

MySQL Features

Features of MySQL MySQL is a relational database management system (RDBMS): It is a collection of many programs and makes relations with many other programs. Easy to use MySQL database: MySQL...

2 minutes read.

MySQL COALESCE function

This statement displays the first non-null value of the table data. The COALESCE function returns a null value when all values of the table are null or do not find...

3 minutes read.

MySQL Database Introduction

MySQL - Database Introduction: The database plays an essential role in MySQL for storing and modifying the data. The database creates and keeps multiple tables. The database organizes and manipulates...

8 minutes read.

MySQL HEX() function

In this context, we will learn how we can use the MySQL HEX() function with proper syntax and good examples. Introduction of MySQL HEX() function For returning an equivalent hexadecimal string value...

3 minutes read.

MySQL DATEDIFF() function

In this context, we will learn how we can use the MySQL DATEDIFF() function with proper syntax and good examples. Introduction of MySQL DATEDIFF() function DATEDIFF() function in MySQL is used to...

3 minutes read.