Combining Aggregate Functions
All the examples of aggregate function used thus far have involved a single function. But actually, SELECT statements may contain as few or as many aggregate functions as needed. Look at this example:
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;
num_items price_min price_max price_avg ---------- --------------- --------------- --------- 7 3.4900 11.9900 6.0614
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).
Caution
Naming Aliases When specifying alias names to contain ...
Get Sams Teach Yourself SQL in 10 Minutes now with O’Reilly online learning.
O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.