Home Functions SQRT()
SQL Numeric Function

SQRT()

SQRT() is a numeric function that returns the square root of a number, the value which multiplied by itself gives the original input.

MySQLPostgreSQLSQL ServerSQLite
Returns: A floating point number (DOUBLE) equal to the square root of the input. Returns NULL when the input is NULL.

Syntax

SQRT(number)
ParameterTypeRequiredDescription
number numeric expression yes The value whose square root is returned. Must be zero or positive on most engines; a negative value raises an error or returns NULL.

How it works

SQRT() returns the non-negative square root of its argument. For an input x it returns the value r such that r * r = x. The result is always a floating point number, even when the input is a whole number and the root is exact, so SQRT(9) comes back as 3 stored as a DOUBLE.

It is mathematically the same as raising a number to the power of one half, so SQRT(x) is equivalent to POWER(x, 0.5). Most engines provide the dedicated SQRT() function because it reads more clearly and can be marginally faster than the general power routine.

SQRT() shows up constantly in distance and geometry work, most famously in the Euclidean distance formula SQRT(dx*dx + dy*dy), and in statistics, where the standard deviation is the square root of the variance. Pair it with ABS() when a value could be negative and you only care about its magnitude before taking a root.

Examples

Square root of a single value

SELECT SQRT(144) AS result;
Result
result
------
    12

Euclidean distance between two points

-- straight line distance from (x1,y1) to (x2,y2)
SELECT SQRT(POWER(x2 - x1, 2) + POWER(y2 - y1, 2)) AS distance
FROM segments
WHERE id = 1;
Result
distance
--------
       5

Square root of a column

SELECT id, area, SQRT(area) AS side_length
FROM squares
ORDER BY id;
Result
id | area | side_length
---+------+------------
 1 |    4 |           2
 2 |   25 |           5
 3 |   90 |    9.486832

Relationship to POWER

-- SQRT(x) is the same as raising x to the power 0.5
SELECT SQRT(50)        AS with_sqrt,
       POWER(50, 0.5)  AS with_power;
Result
with_sqrt | with_power
----------+-----------
 7.071068 |  7.071068

Distance from a fixed origin for every row

-- rank stores nearest to the (0,0) origin
SELECT name,
       SQRT(lat*lat + lng*lng) AS dist
FROM stores
ORDER BY dist ASC;
Result
name    | dist
--------+---------
 Center |  0.5000
 North  |  3.2016
 East   | 12.8400

Common mistakes

Wrong
-- Negative input: errors in SQL Server and PostgreSQL,
-- returns NULL in MySQL and SQLite
SELECT SQRT(-9);
Right
-- Guard the input so a negative can never reach SQRT.
-- Use ABS() if you only want the magnitude, or a CASE
-- to return NULL deliberately.
SELECT CASE WHEN x >= 0 THEN SQRT(x) END AS root
FROM measurements;

The square root of a negative number is not a real number. Behaviour differs by engine: PostgreSQL and SQL Server raise a domain error, while MySQL and SQLite return NULL. Never assume a value is non-negative; validate it or wrap it with ABS() first.

Wrong
-- Expecting an exact decimal, then comparing for equality
SELECT * FROM t
WHERE SQRT(area) = 9.486833;
Right
-- SQRT returns a floating point value, so compare with a
-- tolerance instead of testing exact equality
SELECT * FROM t
WHERE ABS(SQRT(area) - 9.486833) < 0.00001;

SQRT() returns a floating point (DOUBLE) result that is often an unending decimal, so testing it with = almost never matches. Compare against a small tolerance, or round both sides, rather than relying on exact equality.

Performance

SQRT() is a cheap scalar operation, but calling it inside a WHERE clause on an indexed column defeats the index, because the optimiser cannot use an index on col to satisfy a predicate on SQRT(col). Where possible, rewrite the comparison to keep the column bare, for example square both sides so SQRT(area) < 5 becomes area < 25.

For distance filtering, the same trick avoids the root entirely: to find rows within radius r, compare the squared distance dx*dx + dy*dy against r*r and skip SQRT() until you actually need the distance value for display. This is faster and, for a range test, mathematically identical.

Interview questions

What does the SQL SQRT() function return?

It returns the non-negative square root of its numeric argument as a floating point value. SQRT(x) gives the number r where r * r = x. It returns NULL for a NULL input.

How is SQRT() related to POWER()?

They are equivalent: SQRT(x) is the same as POWER(x, 0.5), since a square root is just raising a number to the power of one half. Engines still provide SQRT() because it is clearer to read and can be slightly faster.

SELECT SQRT(50) AS a, POWER(50, 0.5) AS b;

What happens when you pass a negative number to SQRT()?

The result is not a real number, so behaviour depends on the engine. PostgreSQL and SQL Server raise a domain or invalid argument error, while MySQL and SQLite return NULL. Validate the input or apply ABS() before calling SQRT() if a negative value is possible.

How would you compute the distance between two points in SQL?

Use the Euclidean distance formula with SQRT: SQRT(POWER(x2 - x1, 2) + POWER(y2 - y1, 2)). For pure filtering by radius you can compare the squared distance to the squared radius and avoid SQRT altogether.

SELECT SQRT(POWER(x2 - x1, 2) + POWER(y2 - y1, 2)) AS distance
FROM segments;

Why should you avoid comparing SQRT() results with equality?

SQRT() returns a floating point number that is frequently a non-terminating decimal, so an exact = test rarely matches. Compare against a small tolerance or round both sides instead.

Master SQL, one function at a time

Browse the full SQL functions library, or learn the fundamentals with our free, structured courses.