Summary
In this lesson, you learned how to work with date and time values in MySQL. In the
|
Lesson 9. Summarizing DataIn this lesson, you learn how to use MySQL's aggregate functions to produce summary information in a query. |
Aggregate Functions
MySQL provides five aggregate functions that enable you to summarize table data without retrieving every row. By using these functions, you can find the total number of rows in a dataset, the sum of the values, or the highest,
The COUNT() Function
The
COUNT()
function counts values in a query. You can perform a count on a single column by
The following example counts the total number of orders that have been placed in the sample database:
mysql> SELECT COUNT (*) -> FROM orders; +----------+ COUNT(*) +----------+ 7 +----------+ 1 row in set (0.00 sec)
To find the number of unique values in a column, use
COUNT()
with the
DISTINCT
keyword. The following example finds the total number of customer contacts in the database and also the number of unique last
mysql> SELECT COUNT(*), COUNT(DISTINCT last_name) -> FROM customer_contacts; +----------+---------------------------+ COUNT(*) COUNT(DISTINCT last_name) +----------+---------------------------+ 11 10 +----------+---------------------------+ 1 row in set (0.02 sec) The SUM() FunctionThe SUM() function computes a total from the data in a specified column. For example, you can use the following query to return the total number of units ordered of a particular product:
mysql> SELECT SUM(quantity) -> FROM order_lines -> WHERE product_code = 'MAXI'; +---------------+ SUM(quantity) +---------------+ 48 +---------------+ 1 row in set (0.00 sec) If you include an expression within SUM() , it is evaluated for each row of data and the total result is returned, as shown in the following query:
mysql> SELECT SUM(quantity + 1) -> FROM order_lines -> WHERE product_code = 'MAXI'; +-------------------+ SUM(quantity + 1) +-------------------+ 54 +-------------------+ 1 row in set (0.00 sec)
You can use
SUM()
with a conditional expression to return the number of rows that meet the condition. Because the expression
mysql> SELECT SUM(weight < 5) -> FROM products; +-----------------+ SUM(weight < 5) +-----------------+ 2 +-----------------+ 1 row in set (0.00 sec) The AVG() FunctionYou can use the AVG() function to compute an average of the data in a specified column. The following query returns the average weight of all the items in the products table:
mysql> SELECT AVG(weight) -> FROM products; +-------------+ AVG(weight) +-------------+ 4.666667 +-------------+ 1 row in set (0.00 sec)
The
AVG()
function computes a
mean
average. The same result could be performed using
SUM()
and
COUNT()
, as
mysql> SELECT SUM(weight) / COUNT(weight) -> FROM products; +-----------------------------+ SUM(weight) / COUNT(weight) +-----------------------------+ 4.6667 +-----------------------------+ 1 row in set (0.00 sec)
The MIN() and MAX() FunctionsThe MIN() and MAX() functions return the smallest and greatest values from a column, respectively. The following query finds the least expensive product in the database:
mysql> SELECT MIN(price) -> FROM products; +------------+ MIN(price) +------------+ 5.99 +------------+ 1 row in set (0.00 sec)
The
mysql> SELECT MAX(order_date) -> FROM orders; +-----------------+ MAX(order_date) +-----------------+ 2006-02-05 +-----------------+ 1 row in set (0.01 sec) |