MySQL SQRT() Function
The MySQL SQRT() function returns the square root of a given number. In special cases it returns the following:
- If the number is a negative value, then NULL is returned.
Syntax
SQRT(number)
Parameters
number |
Required. Specify the number. Must be a non-negative value. |
Return Value
Returns the square root of a given number.
Example 1:
The example below shows the usage of SQRT() function.
mysql> SELECT SQRT(2); Result: 1.4142135623730951 mysql> SELECT SQRT(3); Result: 1.7320508075688772 mysql> SELECT SQRT(10); Result: 3.1622776601683795 mysql> SELECT SQRT(50); Result: 7.0710678118654755 mysql> SELECT SQRT(100); Result: 10 mysql> SELECT SQRT(-1); Result: NULL
Example 2:
Consider a database table called Sample with the following records:
Data | x |
---|---|
Data 1 | 0.5 |
Data 2 | 1 |
Data 3 | 5 |
Data 4 | 10 |
Data 5 | 50 |
The statement given below can be used to calculate the square root of column x.
SELECT *, SQRT(x) AS SQRT_Value FROM Sample;
This will produce the result as shown below:
Data | x | SQRT_Value |
---|---|---|
Data 1 | 0.5 | 0.7071067811865476 |
Data 2 | 1 | 1 |
Data 3 | 5 | 2.23606797749979 |
Data 4 | 10 | 3.1622776601683795 |
Data 5 | 50 | 7.0710678118654755 |
❮ MySQL Functions