SQRT

Returns the square root value of a non-negative numeric expression.

Syntax

SQRT( expr )

Arguments

Parameter

Description

expr

Non-negative numeric expression

Returns

Always returns a floating point result of the square root

Notes

  • If the value is NULL, the result is NULL.

  • If the numeric expression is negative (<0), an error will be shown.

Examples

For these examples, consider the following table and contents:

CREATE TABLE cool_numbers(i INT, f DOUBLE);

INSERT INTO cool_numbers VALUES (1,1.618033), (-12, -34)
, (22, 3.141592), (-26538, 2.7182818284)
, (NULL, NULL), (NULL,1.4142135623)
, (42,NULL), (-42, NULL)
, (-474, 365);
numbers=> SELECT SQRT(2);
sqrt
------------------
1.4142135623730951

Note

  • Some clients may show fewer digits. See your client settings to change the precision shown.

Replacing negative values with NULL

Combine with CASE to filter out negative results:

numbers=> SELECT i, SQRT(CASE WHEN i>=0 THEN i ELSE NULL END) FROM cool_numbers;
i      | sqrt
-------+-----
     1 |    1
   -12 |
    22 | 4.69
-26538 |
       |
       |
    42 | 6.48
   -42 |
  -474 |