Compute the non-negative square root of a non-negative number.
SQRT(expr)
## Overview Returns the non-negative principal square root of the input. SQRT is a foundational primitive for Euclidean distance, standard deviation (as SQRT of variance), root-mean-square error, and any quadratic-form calculation. Because real square roots are defined only for non-negative inputs, SQRT's domain is restricted. A negative input returns NULL rather than raising an error, so callers that depend on early detection of bad data should validate inputs explicitly or wrap the call in a CASE expression. ## Behavior - Domain: [0, infinity). Negative values return NULL. - Range: [0, infinity). - Returns NULL if the argument is NULL. - SQRT(0) is exactly 0. - SQRT of an exact perfect square (1, 4, 9, 16, ...) returns an exact integer in floating-point. - Integer inputs are implicitly cast to DOUBLE before the call. ## Numeric precision - Uses the IEEE 754 double-precision sqrt routine, correctly rounded to within 0.5 ULP (the strictest accuracy guarantee in the IEEE 754 standard). - For well-formed inputs, SQRT is one of the most numerically stable scalar functions available. - SQRT(POW(x, 2)) returns |x|, not x. ## Compatibility - Conforms to the SQL standard definition of SQRT as a scalar DOUBLE function with domain x >= 0. - Matches typical mathematical library implementations.
| Name | Type | Description |
|---|---|---|
expr | Specifies the non-negative input whose square root is returned. Must be greater than or equal to 0. Negative values return NULL. |
-- Basic literal
SELECT SQRT(4);
-- Result: 2.0
-- Irrational result
SELECT SQRT(2);
-- Result: 1.4142135623730951
-- SQRT of zero
SELECT SQRT(0);
-- Result: 0.0
-- Domain violation: negative input returns NULL
SELECT SQRT(-1);
-- Result: NULL
-- Identity: SQRT(x) equals POW(x, 0.5)
SELECT SQRT(9), POW(9, 0.5);
-- Result: 3.0, 3.0
-- Euclidean distance in 2D
SELECT SQRT(POWER(x2 - x1, 2) + POWER(y2 - y1, 2)) AS distance
FROM geometry.samples.segments;