SQL SQRT function
Returns the square root of a non-negative number. Engines differ on what a negative input does: an error on some, NULL on others.
- Catégorie
- Math
- Returns
float (numeric for numeric input on PostgreSQL, NUMBER on Oracle)- In this category
- 7 of 82
Syntaxe
SQRT(number)
Prise en charge des moteurs
| Moteur | Support | Notes |
|---|---|---|
| SQL Server | Oui | Supported under this name. |
| PostgreSQL | Oui | Supported under this name. |
| MySQL | Oui | Supported under this name. |
| SQLite | Oui | math functions from SQLite 3.35+ |
| Oracle | Oui | Supported under this name. |
Exemples
Any engine
SELECT SQRT(2) AS root2; -- 1.4142...
PostgreSQL
SELECT CASE WHEN x >= 0 THEN SQRT(x) END AS safe_root FROM t;
Bon à savoir
- Negative input: SQL Server, PostgreSQL and Oracle raise an error; MySQL and SQLite return NULL. Guard with CASE WHEN x >= 0 THEN SQRT(x) END.
- The result is not an integer even for perfect squares: SQRT(4) is the float 2 on SQL Server, MySQL and SQLite, and numeric 2.000000000000000 for a numeric input on PostgreSQL.
- A NULL input returns NULL everywhere; it does not error.
Questions about SQRT
What does SQRT(-1) return?
Depends on the engine: an error on SQL Server, PostgreSQL and Oracle; NULL on MySQL and SQLite. Wrap it in CASE WHEN x >= 0 THEN SQRT(x) END when negatives are possible.
Does SQRT return an integer for perfect squares?
No. It returns a float (or a numeric on PostgreSQL and Oracle) even when the root is whole. CAST it to an integer type if that matters.
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: SQL Server, PostgreSQL, MySQL, SQLite, 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.