SQL REGR_R2 function
Returns the coefficient of determination of the least-squares fit, the share of y's variance the line explains.
- Catégorie
- Aggregates
- Returns
numeric (0 to 1)- In this category
- 31 of 52
Syntaxe
REGR_R2(y, x) | REGR_R2(y, x) OVER (...)
Prise en charge des moteurs
| Moteur | Support | Notes |
|---|---|---|
| SQL Server | Not built in | same manual route |
| PostgreSQL | Oui | Supported under this name. |
| MySQL | Not built in | square the manual correlation coefficient |
| SQLite | Not built in | No built-in equivalent. |
| Oracle | Oui | Supported under this name. |
Exemples
PostgreSQL
SELECT regr_r2(sales, ad_spend) FROM monthly;
Oracle
SELECT REGR_R2(salary, age) FROM employees;
Bon à savoir
- NULL on fewer than two complete pairs or when every x is the same. When every y is the same but x varies, it returns 1 (a flat line fits perfectly) even though CORR returns NULL.
- Otherwise it is the square of CORR(y, x), so the sign of the relationship is lost; pair it with REGR_SLOPE to read direction.
- Only complete (y, x) pairs are used, like the rest of the REGR_ family.
Questions about REGR_R2
What is a good REGR_R2 value?
Closer to 1 means the line explains more of y's variance; there is no universal cutoff: it depends on the domain.
Is REGR_R2 just CORR squared?
For ordinary data, yes. The exception is a constant y with varying x: CORR is NULL there, REGR_R2 returns 1.
Which engines have it?
PostgreSQL and Oracle; elsewhere square the manual correlation formula.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: PostgreSQL, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.