# CORR

Syntax

Description of the illustration corr.gif

"Analytic Functions" for information on syntax, semantics, and restrictions

Purpose

`CORR` returns the coefficient of correlation of a set of number pairs. You can use it as an aggregate or analytic function.

This function takes as arguments any numeric datatype or any nonnumeric datatype that can be implicitly converted to a numeric datatype. Oracle determines the argument with the highest numeric precedence, implicitly converts the remaining arguments to that datatype, and returns that datatype.

Table 2-10, "Implicit Type Conversion Matrix" for more information on implicit conversion and "Numeric Precedence" for information on numeric precedence

Oracle Database applies the function to the set of (`expr1`, `expr2`) after eliminating the pairs for which either `expr1` or `expr2` is null. Then Oracle makes the following computation:

```COVAR_POP(expr1, expr2) / (STDDEV_POP(expr1) * STDDEV_POP(expr2))

```

The function returns a value of type `NUMBER`. If the function is applied to an empty set, then it returns null.

Note:

The `CORR` function calculates the Pearson's correlation coefficient, which requires numeric expressions as arguments. Oracle also provides the `CORR_S` (Spearman's rho coefficient) and `CORR_K` (Kendall's tau-b coefficient) to support nonparametric or rank correlation.

"Aggregate Functions", "About SQL Expressions" for information on valid forms of `expr`, and CORR_* and CORR_S

Aggregate Example

The following example calculates the coefficient of correlation between the list prices and minimum prices of products by weight class in the sample table `oe.product_information`:

```SELECT weight_class, CORR(list_price, min_price)
FROM product_information
GROUP BY weight_class;

WEIGHT_CLASS CORR(LIST_PRICE,MIN_PRICE)
------------ --------------------------
1                  .99914795
2                 .999022941
3                 .998484472
4                 .999359909
5                 .999536087
```

Analytic Example

The following example shows the correlation between duration at the company and salary by the employee's position. The result set shows the same correlation for each employee in a given job:

```SELECT employee_id, job_id,
TO_CHAR((SYSDATE - hire_date) YEAR TO MONTH ) "Yrs-Mns",     salary,
CORR(SYSDATE-hire_date, salary)
OVER(PARTITION BY job_id) AS "Correlation"
FROM employees
WHERE department_id in (50, 80)
ORDER BY job_id, employee_id;

EMPLOYEE_ID JOB_ID     Yrs-Mns     SALARY Correlation
----------- ---------- ------- ---------- -----------
145 SA_MAN     +08-07       14000  .912385598
146 SA_MAN     +08-04       13500  .912385598
147 SA_MAN     +08-02       12000  .912385598
148 SA_MAN     +05-07       11000  .912385598
149 SA_MAN     +05-03       10500  .912385598
150 SA_REP     +08-03       10000   .80436755
151 SA_REP     +08-02        9500   .80436755
152 SA_REP     +07-09        9000   .80436755
153 SA_REP     +07-01        8000   .80436755
154 SA_REP     +06-05        7500   .80436755
155 SA_REP     +05-06        7000   .80436755
...
```