BigQuery · Math function
BigQuery SQRT
SQRT(x) returns the square root of x: SQRT(16) is 4.0. INT64 and FLOAT64 inputs return FLOAT64, while NUMERIC stays NUMERIC. A negative input fails with 'Argument to SQRT cannot be negative'; SAFE.SQRT returns NULL instead.
SQRT computes the principal square root. The return type is FLOAT64 for INT64 and FLOAT64 input, even for perfect squares (SQRT(16) is 4.0), and NUMERIC or BIGNUMERIC for those types (SQRT(NUMERIC '2.25') is 1.5).
Any negative input raises Argument to SQRT cannot be negative and fails the whole query. Values that should be zero but come out as a tiny negative number from floating-point arithmetic, such as a variance computed by hand, trigger it too. Clamp with SQRT(GREATEST(x, 0)), or use SAFE.SQRT(x), which returns NULL.
Common uses are root mean square error, SQRT(AVG(POW(actual - forecast, 2))), and Euclidean distance on projected coordinates. For standard deviation, the STDDEV aggregates already exist, and for latitude and longitude, ST_DISTANCE returns meters; SQRT over degree differences doesn't.
Syntax
SQRT(X)Arguments and return value
| Argument | Description |
|---|---|
X | INT64, NUMERIC, BIGNUMERIC or FLOAT64. Must be 0 or greater. |
Returns: FLOAT64 for INT64 or FLOAT64 input, NUMERIC for NUMERIC and BIGNUMERIC for BIGNUMERIC.
Examples
Square roots by input type
SELECT
SQRT(16) AS perfect_square,
SQRT(2) AS irrational,
SQRT(NUMERIC '2.25') AS numeric_input;| perfect_square | irrational | numeric_input |
|---|---|---|
| 4.0 | 1.4142135623730951 | 1.5 |
Root mean square error of a forecast
The errors are 10, -5 and 2. Their squares average 43, and the square root is about 6.56 orders.
WITH forecasts AS (
SELECT DATE '2026-11-20' AS day, 120 AS actual_orders, 110 AS forecast_orders UNION ALL
SELECT DATE '2026-11-21', 95, 100 UNION ALL
SELECT DATE '2026-11-22', 130, 128
)
SELECT
ROUND(SQRT(AVG(POW(actual_orders - forecast_orders, 2))), 2) AS rmse
FROM forecasts;| rmse |
|---|
| 6.56 |
Negative inputs
SQRT(-4) would fail the query. SAFE.SQRT returns NULL, and GREATEST clamps a tiny negative rounding error to zero.
SELECT
SAFE.SQRT(-4) AS safe_sqrt,
SQRT(GREATEST(-1e-12, 0)) AS clamped_at_zero;| safe_sqrt | clamped_at_zero |
|---|---|
| NULL | 0.0 |
Common mistakes
- Feeding values that can be slightly negative. A hand-computed variance such as
AVG(x * x) - AVG(x) * AVG(x)can come out a hair below zero; clamp it withGREATEST(..., 0)or use STDDEV_POP. - Expecting exact results.
SQRT(2) * SQRT(2)is 2.0000000000000004 in FLOAT64; round before comparing. - Computing distances from latitude and longitude with SQRT. Degrees aren't equal distances; use
ST_DISTANCE(ST_GEOGPOINT(lon1, lat1), ST_GEOGPOINT(lon2, lat2)). - Expecting INT64 for perfect squares.
SQRT(16)returns the FLOAT64 4.0; cast it if you need an integer.
FAQ
How do I calculate standard deviation in BigQuery?
Use the STDDEV_SAMP (alias STDDEV) or STDDEV_POP aggregate functions instead of SQRT over a manual variance. SQRT(VAR_SAMP(x)) returns the same value as STDDEV_SAMP(x).
How do I avoid the 'Argument to SQRT cannot be negative' error?
Use SAFE.SQRT(x) to get NULL for negative input, or SQRT(GREATEST(x, 0)) when small negative values are rounding noise that should count as zero.
How do I compute a cube root in BigQuery?
Use CBRT(x), which also accepts negative numbers (CBRT(-8) is -2.0), or POW(x, 1/3) for values of 0 or more.
Related
More mathematical functions
All BigQuery mathematical functions · All functions · BigQuery error messages
