Aggregate Functions | ||||||||||||||
Aggregate functions return a single value based upon a set of other values. If used among many other expressions in the item list of a SELECT statement, the SELECT must have a GROUP BY clause. No GROUP BY clause is required if the aggregate function is the only value retrieved by the SELECT statement. The supported aggregate functions and their syntax are listed in Table 4.1 . |
||||||||||||||
SQL99 Aggregate Functions | ||||||||||||||
|
||||||||||||||
Technically speaking, ANY, EVERY, and SOME are considered aggregate functions. However, they have been discussed as range search criteria since they are most often used that way. Refer to the SELECT . . . WHERE topic in the previous chapter for more information on these functions. The number of values processed by an aggregate varies depending on the number of rows queried from the table. This behavior makes aggregate functions different from scalar functions, which require a fixed number and fixed type of parameters. The general syntax of an aggregate function is: aggregate_function_name ( [ALL | DISTINCT] expression )The aggregate function name may be AVG, COUNT, MAX, MIN, or SUM. The |
AVG and SUM | |
The AVG and SUM are supported by Microsoft SQL Server, MySQL, Oracle, and PostgreSQL. |
|
Example | |
The following query computes average year-to-date sales for each type of book: SELECT type, AVG( ytd_sales ) AS "average_ytd_sales" FROM titles GROUP BY type; This query returns the sum of year-to-date sales for each type of book: SELECT type, SUM( ytd_sales ) FROM titles GROUP BY type; |
COUNT | |
The COUNT function has three variations. COUNT(*) counts all the rows in the target table whether they include nulls or not. COUNT(expression) computes the number of rows with non-NULL values in a specific column or expression. COUNT(DISTINCT expression) computes the number of distinct non-NULL values in a column or expression. |
|
Examples | |
This query counts all rows in a table: SELECT COUNT(*) FROM publishers; The following query finds the number of different countries where publishers are located: SELECT COUNT(DISTINCT country) "Count of Countries" FROM publishers |
MIN and MAX | |
MIN(expression) and MAX(expression) find the minimum and maximum value (string, datetime, or numeric) in a set of rows. DISTINCT or ALL may be used with these functions, but they do not affect the result. MIN and MAX are supported by Microsoft SQL Server, MySQL, Oracle, and PostgreSQL. MySQL also supports the functions LEAST( ) and GREATEST( ), providing the same capabilities. |
|
Examples | |
The following query finds the best and worst sales for any title on record: SELECT 'MIN' = MIN(ytd_sales), 'MAX' = MAX(ytd_sales) FROM titles; Aggregate functions are used often in the SELECT type 'Category', AVG( price ) 'Average Price' FROM titles GROUP BY type HAVING AVG(price) > 15 |