MySQL Tutorial

MySQL Tutorial MySQL Features MySQL Database Introduction MySQL Environmental Setup MySQL Data Types MySQL variable MySQL Advance table Query MySQL database queries MySQL Entity-Relationship Model MySQL Table Query MySQL Operators MySQL logical conditions MySQL Queries MySQL Clauses Clustered vs Non-Clustered Index MySQL Full text index MySQL Descending Index MySQL Invisible Index MySQL Composite Index MySQL Prefix index MySQL Index MySQL Create index MySQL Drop Index MySQL Show index MySQL Unique index MySQL Table MySQL Variable MySQL View MySQL Constraints MySQL Command Line Client Basic Queries MySQL Stored Procedure MySQL IF Statement MySQL Subquery MySQL Triggers

MySQL Join

MySQL Join MySQL CROSS JOIN MySQL DELETE JOIN MySQL EQUI JOIN MySQL INNER JOIN MySQL Union MySQL NATURAL JOIN MySQL RIGHT JOIN MySQL SELF JOIN MySQL UPDATE JOIN

MySQL Function

MySQL Function MySQL AVG() Function MySQL SUM() Function MySQL String() Function MySQL Advance() Function MySQL Aggregate() Function MySQL COALESCE() Function MySQL Control Flow Function MySQL COUNT() Function MySQL Date And Time Function MySQL GREATEST() Function MySQL ISNULL() Function MySQL LEAST() Function MySQL Math() Function MySQL MAX() Function MySQL MIN() Function MySQL find_in_set() function MySQL ASIN() Function MySQL CEIL() function MySQL CEILING() function MySQL TAN() Function MySQL Truncate() Function MySQL FLOOR() function MySQL LN() function MySQL LOG2() function MySQL LOG10() function MySQL MOD() function MySQL PI() function MySQL POW() function MySQL RADIANS() function MySQL RAND() function MySQL ROUND() function MySQL Character Length Function MySQL Current Date Function MySQL Date Add Function MySQL Date Format Function MySQL Datediff Function MySQL Day Function MySQL Elt Function MySQL Export Set Function MySQL Field Function MySQL Format Function MySQL From Base64 Function MySQL Hex Function MySQL Insert Function MySQL Instr Function MySQL Length Function MySQL CONCAT() function MySQL FIND_IN_SET() function MySQL LIKE() function MySQL LOAD_FILE() function MySQL LOCATE() function MySQL LOG() function MySQL MONTHNAME() function MySQL NOW() function MySQL PERIOD_ADD() function MySQL PERIOD_DIFF() function MySQL POWER() function MySQL QUARTER() function MySQL REVERSE() function MySQL RIGHT() Function MySQL RPAD() function MySQL RTRIM() function MySQL SEC_TO_TIME() function MySQL SOUNDEX() function

Questions

Which Clause is Similar to Having Clause in MySQL

Misc

MySQL Error 1046 - No Database Selected Failed to Start MySQL Service Unit MySQL Service Unit not Found Import MySQL Connector Mudule not Found Error No Module Named MySQL Joins Available in MySQL MySQL Docs MySQL Download For Windows 7 64 Bit MySQL Error Code 1064 MySQL Export MySQL History MySQL Host MySQL Import MySQL Drop All Tables MySQL Drop MySQL Error Code 1175 MySQL Events MySQL Except MYSQL Foreign Key Constraint MySQL If Exists MySQL IndexOf MySQL List All Tables json_extract in MySQL TIMESTAMPDIFF in MySQL MySQL Syntax Checker Sudo MySQL Secure Installation

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.