Database Reference
In-Depth Information
Input
SELECT COUNT(*) AS num_items,
MIN(prod_price) AS price_min,
MAX(prod_price) AS price_max,
AVG(prod_price) AS price_avg
FROM products;
Output
+-----------+-----------+-----------+-----------+
| num_items | price_min | price_max | price_avg |
+-----------+-----------+-----------+-----------+
| 14 | 2.50 | 55.00 | 16.133571 |
+-----------+-----------+-----------+-----------+
Analysis
Here a single SELECT statement performs four aggregate calculations in one step
and returns four values (the number of items in the products table; and the
highest, lowest, and average product prices).
Tip
Naming Aliases When specifying alias names to contain the results of an aggregate
function, try not to use the name of an actual column in the table. Although there is
nothing actually illegal about doing so, using unique names makes your SQL easier to
understand and work with (and troubleshoot in the future).
Summary
Aggregate functions are used to summarize data. MariaDB supports a range of
aggregate functions, all of which can be used in multiple ways to return just the
results you need. These functions are designed to be highly efficient, and they
usually return results far more quickly than you could calculate them yourself
within your own client application.
 
 
Search WWH ::




Custom Search