MariaDB: AVG Function
This MariaDB tutorial explains how to use the MariaDB AVG function with syntax and examples.
Description
The MariaDB AVG function returns the average value of an expression.
Syntax
The syntax for the AVG function in MariaDB is:
SELECT AVG(aggregate_expression) FROM tables [WHERE conditions];
OR the syntax for the AVG function when grouping the results by one or more columns is:
SELECT expression1, expression2, ... expression_n, AVG(aggregate_expression) FROM tables [WHERE conditions] GROUP BY expression1, expression2, ... expression_n;
Parameters or Arguments
- expression1, expression2, ... expression_n
- Expressions that are not encapsulated within the AVG function and must be included in the GROUP BY clause at the end of the SQL statement.
- aggregate_expression
- This is the column or expression that will be averaged.
- tables
- The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
- WHERE conditions
- Optional. These are conditions that must be met for the records to be selected.
Applies To
The AVG function can be used in the following versions of MariaDB:
- MariaDB 10
Example - With Single Expression
Let's look at some MariaDB AVG function examples and explore how to use the AVG function in MariaDB.
First, we'll go through an example of how to use the AVG function with a single expression in MariaDB.
For example:
SELECT AVG(file_size) AS "Average File Size" FROM pages WHERE site_name = 'TechOnTheNet.com';
In this AVG function example, we will calculate the average file size where the site_name is 'TechOnTheNet.com'. We've aliased the AVG(file_size) expression as "Average File Size". As a result, "Average File Size" will display as the column title when the result set is returned.
Example - Using DISTINCT
Next, let's look at how to use the DISTINCT clause within the AVG function in MariaDB.
For example:
SELECT AVG(DISTINCT file_size) AS "Averaging Unique File Sizes" FROM pages WHERE site_name = 'TechOnTheNet.com';
In this AVG example, if there were two file_size values that were the same, only one of these values would be used in the AVG function calculation.
Example - Using Formula
The expression contained within the AVG function does not need to be a single field. You could also use a formula. Let's look at how to use a formula in the AVG function in MariaDB.
For example:
SELECT AVG(file_size * 1.1) AS "Average" FROM pages WHERE site_name = 'CheckYourMath.com';
In this AVG function example, the file_size column would be multiplied it by 1.1 and the result would be averaged.
Example - Using GROUP BY
Finally, let's look at how to use the GROUP BY clause with the AVG function in MariaDB.
If you are returning columns that are no encapsulated in the AVG function, you must use the GROUP BY clause.
For example:
SELECT site_id, AVG(file_size) AS "Average File Size" FROM pages WHERE site_name = 'BigActivities.com' GROUP BY site_id;
In this AVG function example, we must use a GROUP BY clause because the site_id field is not encapsulated in the AVG function. The site_id column must, herefore, be listed in the GROUP BY section at the end of the SQL statement.
Advertisements