In SQL, the SQRT
function returns the square root of a number.
Syntax #
SQRT(number | expression)
Code language: SQL (Structured Query Language) (sql)
Argument #
number | expression is a number or expression that evaluates to a number.
The SQRT
function only accepts a positive number. If you pass a negative number to the function, it will issue an error.
Return type #
The SQRT
function a float number with the precision that depends on each database system.
Examples #
The following statement returns the square root of 100.
SELECT
SQRT (100) result;
Code language: SQL (Structured Query Language) (sql)
result
--------
10
Code language: SQL (Structured Query Language) (sql)
The following example uses the SQRT with an expression:
SELECT
SQRT(2 * 2) result;
Code language: SQL (Structured Query Language) (sql)
Output:
result
--------
2
The following example attempts to use a negative number with the SQRT
function and causes an error:
SELECT
SQRT (-1) result;
Code language: SQL (Structured Query Language) (sql)
Error:
ERROR: cannot take square root of a negative number
Code language: HTTP (http)
Was this tutorial helpful ?