// math reference

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.

Kategorie
Math
Returns
float (numeric for numeric input on PostgreSQL, NUMBER on Oracle)
In this category
7 of 82

Syntax

SQRT(number)

Engine-Unterstützung

EngineSupportNotizen
SQL Server Ja Supported under this name.
PostgreSQL Ja Supported under this name.
MySQL Ja Supported under this name.
SQLite Ja math functions from SQLite 3.35+
Oracle Ja Supported under this name.

Beispiele

Any engine

SELECT SQRT(2) AS root2;  -- 1.4142...

PostgreSQL

SELECT CASE WHEN x >= 0 THEN SQRT(x) END AS safe_root FROM t;

Gut zu wissen

  • 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.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

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.