## 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 |

)`independent` | Computes a correlation coefficient |

| Counts the rows defined by the |

| Counts all rows in the specified table or view |

)`independent` | Computes population covariance |

)`independent` | Computes sample covariance |

`WITHIN GROUP` (`ORDER BY` )`sort_list` | 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 |

`WITHIN GROUP` (`ORDER BY` )`sort_list` | Generates a dense rank (no ranks are skipped) for a hypothetical row ( GROUP BY |

| Finds the minimum value in a column given by |

| Finds the maximum value in a column given by |

`WITHIN GROUP` (`ORDER BY` )`sort_list` | Generates a relative rank for a hypothetical row by dividing that row’s rank less 1 by the number of rows in the group |

`WITHIN GROUP` (`ORDER BY` )`sort_list` | Generates an interpolated value that, if added to the group, would correspond to the |

`WITHIN GROUP` (`ORDER BY` )`sort_list` | Returns the value with the smallest cumulative distribution value greater than or equal to |

`WITHIN GROUP` (`ORDER BY` )`sort_list` | Generates a rank for a hypothetical row ( GROUP BY |

)`independent` | Computes the average of the independent variable |

)`independent` | Computes the average of the dependent variable |

)`independent` | Counts the number of pairs remaining in the group after any pair with one or more NULL values has been eliminated |

)`independent` | Computes the y-intercept of the least-squares-fit linear equation |

)`independent` | Squares the correlation coefficient |

)`independent` | Determines the slope of the least-squares-fit linear equation |

)`independent` | Sums the squares of the independent variables |

)`independent` | Sums the products of each pair of variables |

)`independent` | 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:

( [ALL | DISTINCT]`aggregate_function_name`

)`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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.