Database Reference
In-Depth Information
Table 12.1 SQL Aggregate Functions
Function
Description
AVG()
Returns a column's average value
COUNT()
Returns the number of rows in a column
MAX()
Returns a column's highest value
MIN()
Returns a column's lowest value
SUM()
Returns the sum of a column's values
The use of each of these functions is explained in the following sections.
Note
Standard Deviation A series of standard deviation aggregate functions
are also sup-
ported by MariaDB but are not covered in the chapters.
The AVG() Function
AVG() is
used to return the average value of a specific column by counting both
the number of rows in the table and the sum of their values. AVG() can be used
to return the average value of all columns or of specific columns or rows.
This first example uses AVG() to return the average price of all the products in
the products table:
Input
SELECT AVG(prod_price) AS avg_price
FROM products;
Output
+-----------+
| avg_price |
+-----------+
| 16.133571 |
+-----------+
Analysis
The previous SELECT statement returns a single value, avg_price , that contains
the average price of all products in the products table. avg_price is an alias as
explained in Chapter 10, “Creating Calculated Fields.”
AVG() can also be used to
determine the average value of specific columns or
rows. The following example returns the average price of products offered by a
specific vendor:
 
 
 
Search WWH ::




Custom Search