To find the square root value of a number in MySQL, you need to use the SQRT()
function.
The syntax of the SQRT()
function is as follows:
SQRT(expression)
The function will return the square root value of the expression passed into it. Only expressions that evaluate into numeric values are valid arguments for the function.
Here’s an example of the SQRT()
function in action:
SELECT SQRT(9);
-- +---------+
-- | SQRT(9) |
-- +---------+
-- | 3 |
-- +---------+
You can pass a literal numeric expression or a column name with a numeric type.
Suppose you have a table called students
with the following data:
+----+-------+-------+
| id | name | score |
+----+-------+-------+
| 1 | Jack | 72 |
| 2 | Susan | 48 |
| 3 | John | 56 |
| 4 | Fany | 81 |
+----+-------+-------+
You can pass the score
column into the SQRT()
function as shown below:
SELECT SQRT(score) FROM students;
-- +-------------------+
-- | SQRT(score) |
-- +-------------------+
-- | 8.48528137423857 |
-- | 6.928203230275509 |
-- | 7.483314773547883 |
-- | 9 |
-- +-------------------+
When you pass a non-numeric expression, the function will return a 0
value.
When you pass a negative number, then SQRT()
function will return a NULL
value
SELECT SQRT(false), SQRT("a string"), SQRT(-9);
-- +-------------+------------------+----------+
-- | SQRT(false) | SQRT("a string") | SQRT(-9) |
-- +-------------+------------------+----------+
-- | 0 | 0 | NULL |
-- +-------------+------------------+----------+
And that’s how you can find the square root value of a number in MySQL.
You can pass an expression that evaluates to a numeric value or a table column name with numeric type.