Changes

MySQL Data Aggregation Functions

1,590 bytes added, 18:19, 26 October 2007
Using the MySQL COUNT() Function
| 5 |
+-----------------+
1 row in set (0.00 sec)
</pre>
 
== Using the MySQL MAX() Function ==
 
The MAX() function returns data from the row in which the specified column contains the highest value. For example we can findthe most expensive product in our database table:
 
<pre>
mysql> SELECT MAX(prod_price) AS max_price FROM products;
+-----------+
| max_price |
+-----------+
| 999 |
+-----------+
1 row in set (0.00 sec)
</pre>
 
== Using the MySQL MIN() Function ==
 
The MIN() function performs the opposite task to the MAX() function in that it returns data from the row containing the lowest value in the specified column. For example, to find the least expensive item in our table:
 
<pre>
mysql> SELECT MIN(prod_price) AS min_price FROM products;
+-----------+
| max_price |
+-----------+
| 49 |
+-----------+
1 row in set (0.00 sec)
</pre>
 
== Using the SUM() Function ==
 
The SUM() function returns the total of all the values in a a specified column. Therefore, to get the total value of every item in the table:
 
<pre>
mysql> SELECT SUM(prod_price) AS total_price FROM products;
+-----------+
| max_price |
+-----------+
| 2075 |
+-----------+
1 row in set (0.00 sec)
</pre>
 
== Using Multiple Aggregate Functions ==
 
SELECT statements are not restricted to a single aggregate function. It is perfectly valid to include calls to multiple functions, for example:
 
<pre>
mysql> SELECT MAX(prod_price) AS max_price, MIN(prod_price) AS max_price FROM products;
+-----------+-----------+
| max_price | max_price |
+-----------+-----------+
| 999 | 49 |
+-----------+-----------+
1 row in set (0.00 sec)
</pre>