SQL SQRT Function

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)

Try it

 result
--------
     10Code 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)

Try it

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)

Try it

Error:

ERROR:  cannot take square root of a negative numberCode language: HTTP (http)
Was this tutorial helpful ?