How the SQRT() function works in Mariadb?

The SQRT() function in MariaDB is a mathematical function that calculates the square root of a given number.

Posted on

The SQRT() function in MariaDB is a mathematical function that calculates the square root of a given number. It is a straightforward function that can be very useful in various calculations, particularly in scientific and financial applications.

Syntax

The syntax for the MariaDB SQRT() function is as follows:

SQRT(number)

The SQRT() function takes a single argument, number, which is the numeric value you want to calculate the square root for. The function returns the square root of the number.

Examples

Example 1: Basic Usage of SQRT()

This example demonstrates the basic usage of the SQRT() function to find the square root of a number.

SELECT SQRT(16);

The output for this statement is:

+----------+
| SQRT(16) |
+----------+
|        4 |
+----------+

This indicates that the square root of 16 is 4.

Example 2: SQRT() with Non-Integer Values

In this example, we will calculate the square root of a non-integer value.

SELECT SQRT(20.25);

The output for this statement is:

+-------------+
| SQRT(20.25) |
+-------------+
|         4.5 |
+-------------+

This shows that the square root of 20.25 is 4.5.

Example 3: Using SQRT() in a Table

This example shows how to use the SQRT() function to calculate the square root of values in a table.

DROP TABLE IF EXISTS numbers;
CREATE TABLE numbers (value DECIMAL(10,2));
INSERT INTO numbers VALUES (9), (16), (25);

SELECT value, SQRT(value) AS square_root FROM numbers;

The output for this statement is:

+-------+-------------+
| value | square_root |
+-------+-------------+
|  9.00 |           3 |
| 16.00 |           4 |
| 25.00 |           5 |
+-------+-------------+

This demonstrates the use of SQRT() to calculate the square root of each value in the table.

Example 4: SQRT() with Negative Numbers

This example illustrates what happens when you use the SQRT() function with a negative number.

SELECT SQRT(-9);

The output for this statement is:

+----------+
| SQRT(-9) |
+----------+
|     NULL |
+----------+

MariaDB returns NULL because the square root of a negative number is not a real number.

Example 5: SQRT() in Complex Expressions

Here, we use the SQRT() function as part of a more complex expression.

SELECT SQRT(144) + 10;

The output for this statement is:

+----------------+
| SQRT(144) + 10 |
+----------------+
|             22 |
+----------------+

This shows that you can use the SQRT() function in conjunction with other operations.

Below are a few functions related to the MariaDB SQRT() function:

  • MariaDB POW() function is used to raise a number to the power of another number.
  • MariaDB ABS() function returns the absolute value of a number, which is useful in conjunction with SQRT() when dealing with complex numbers.

Conclusion

The SQRT() function is a simple yet powerful tool in MariaDB that allows for the calculation of square roots. It is essential for users to understand that it only works with non-negative numbers and will return NULL for negative inputs. By combining SQRT() with other mathematical functions, users can perform a wide range of calculations to suit their data analysis needs.