ANSI SQL Aggregate Functions
Aggregate functions return a single value based upon a set of other values. If used among other expressions in the item list of a SELECT statement, the SELECT must have a GROUP BY or HAVING clause. No GROUP BY or HAVING clause is required if the aggregate function is the only value retrieved by the SELECT statement. The aggregate functions supported by the ANSI SQL standard and their syntax are listed in Table 4-1.
Function | Usage |
| Computes the average value of a column given by |
| Computes a correlation coefficient |
| Counts the rows defined by the |
| Counts all rows in the specified table or view |
| Computes population covariance |
| Computes sample covariance |
| Computes the relative rank of a hypothetical row within a group of rows, where the rank is equal to the number of rows less than or equal to the hypothetical row divided by the number of rows in the group |
| Generates a dense rank (no ranks are skipped) for a hypothetical row ( |
| Finds the minimum value in a column given by |
| Finds the maximum value in a column given by |
| Generates a relative rank for a hypothetical row by dividing that row’s rank less 1 by the number of rows in the group |
| Generates an interpolated value that, if added to the group, would correspond to the |
| Returns the value with the smallest cumulative distribution value greater than or equal to |
| Generates a rank for a hypothetical row ( |
| Computes the average of the independent variable |
| Computes the average of the dependent variable |
| Counts the number of pairs remaining in the group after any pair with one or more NULL values has been eliminated |
| Computes the y-intercept of the least-squares-fit linear equation |
| Squares the correlation coefficient |
| Determines the slope of the least-squares-fit linear equation |
| Sums the squares of the independent variables |
| Sums the products of each pair of variables |
| Sums the squares of the dependent variables |
| Computes the population standard deviation of all |
| Computes the sample standard deviation of all |
| Computes the sum of the column values given by |
| Computes the population variance of all |
| Computes the sample standard deviation of all |
Technically speaking, ALL, ANY, 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 Chapter 3 for more information on these functions.
The number of values processed by an aggregate function varies depending on the number of rows queried from the table. This behavior differentiates aggregate functions from scalar functions, which can only operate on the values of a single row per invocation.
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, as listed in Table 4-1. The ALL keyword, which specifies the default behavior, evaluates all rows when aggregating the value of the function. The DISTINCT keyword uses only distinct values when evaluating the function.
Warning
All aggregate functions except COUNT(*) will ignore NULL values when computing their results.
Get SQL in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.