CORR(expression1, expression2) returns the Pearson correlation of two numbers over the rows where both are present: 1 means they rise together, -1 that one falls as the other rises. In SQL it is corr(Sales, Units). Rows with a null on either side are dropped, not counted as 0.
Updated · Checked on the xmetric engine
What is the syntax?
CORR(expression1, expression2)
expression1, expression2
two numeric fields or row-level calculations, paired row by row.
Returns a real number from -1 to 1, over rows where both values are non-null. Null when there are none.
What does it return, and what is the SQL?
Each example runs on the same 12 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.
How closely do sales follow units?
Tableau
CORR([Sales], [Units])
SQL (DuckDB)
SELECTcorr(Sales, Units) AS result
FROM input
Over the eight lines with both values, 0.985: bigger orders have more units, almost in a straight line.
Result
0.99
✓ The Tableau formula and the SQL return the same 1 rows.
Where does a straight translation go wrong?
Common mistake
Why does filling nulls with 0 weaken the correlation?
Tableau leaves out rows where either value is null. coalesce(..., 0) keeps them as sales of 0 with 1 to 3 units, which drops the correlation to 0.63.
Tableau
CORR([Sales], [Units])
Tempting SQL
SELECTcorr(coalesce(Sales, 0), coalesce(Units, 0)) AS result
FROM input
SQL that matches
SELECTcorr(Sales, Units) AS result
FROM input
Tableau
Tempting SQL
SQL that matches
0.99
0.63
0.99
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
What does CORR return when one side is all null?
Keeping only North's sales leaves no value on that side, since North has none. CORR returns null. Columns 1 to 1 are the sample rows, by id.
Case
Tableau, then SQL (DuckDB)
1
all-null input
CORR(IIF([Region] = 'North', [Sales], NULL), [Units])corr(CASE WHEN Region = 'North' THEN Sales END, Units)
null
✓ On every row, each Tableau formula and its SQL return the same value.
How do I write it in Snowflake or BigQuery?
The first example, written for each warehouse from its documentation. These are not run here; DuckDB is.