Cloud

sqrt

The sqrt() function returns the square root of a given positive number.

Syntax

The syntax for the sqrt() function in Redpanda SQL is:

SQRT(x)

The sqrt() function requires one argument:

  • x: A positive number or an expression that evaluates to a positive number.

Examples

sqrt() a positive value

This example demonstrates how to find the square root of a positive integer with sqrt():

SELECT SQRT(81);

The returned result:

+-----+
| f   |
+-----+
| 9   |
+-----+

sqrt() with an expression

This example shows how to use the sqrt() function to find the square root of the result of an expression:

SELECT SQRT(60 + 4);

The result of this statement is the square root of 64:

+-----+
| f   |
+-----+
| 8   |
+-----+

sqrt() with double precision result

In addition to integers, Redpanda SQL also supports calculating square roots with floating-point numbers as the outcome, as shown in this example:

SELECT SQRT(70);

The output of this statement is 8.3666, which is the square root of 70 with double precision:

+----------+
| f        |
+----------+
| 8.3666   |
+----------+

sqrt() a negative number

This example demonstrates how attempting to use the sqrt() function with a negative value returns an error:

SELECT SQRT(-25);

As the sqrt() function only accepts positive numbers, it returns a NaN (Not a Number) result for the square root of -25:

+-------+
| f     |
+-------+
| NaN   |
+-------+

SQRT operator (|/(x))

This example uses the SQRT operator (|/(x)) to calculate the square root of a number:

SELECT |/(169) AS sqrt_operator;

This example calculates the square root of 169 using the SQRT operator. The result of this query will be:

 sqrt_operator
---------------
            13