Back to Library

SQRT() in SQL

Returns the square root of a non-negative number. The square root is the value that, when multiplied by itself, gives the number.

Syntax

SQRT(number)
Return type
FLOAT/DOUBLE/NUMERIC (depends on DBMS)

SQRT() Function Example

SELECT SQRT(16);
-- Output: 4

What is SQRT() in SQL?

The SQRT() function in SQL returns the square root of a given non-negative number. The square root of a number is the value that, when multiplied by itself, results in the original number. This function is commonly used in mathematical computations, statistical analysis, financial modeling, and geometric calculations. It is supported in SQL Server, MySQL, PostgreSQL, and Oracle. Since SQRT() only works with non-negative numbers, passing a negative value may result in an error or NULL, depending on the database system.

Parameters:

  • number: The non-negative number to calculate the square root of

Example Use Cases:

-- Calculate distance in 2D plane

SELECT SQRT(POWER(x2-x1, 2) + POWER(y2-y1, 2)) AS distance FROM points;
-- Output:
distance
--------
5.83
7.21
4.47
9.06
6.32

-- Find standard deviation 

SELECT SQRT(variance) AS std_dev FROM statistics; 
-- Output:
std_dev
-------
2.45
3.12
1.98
4.23
2.87

-- Calculate room diagonal 

SELECT SQRT(POWER(length, 2) + POWER(width, 2)) AS diagonal FROM rooms;
-- Output:
diagonal
--------
12.65
15.81
18.97
20.52
25.46

Notes:

  • Behavior: Returns NULL or error for negative inputs (DBMS-specific)

  • Performance Considerations: More computationally intensive than basic arithmetic

  • Version Info: Core mathematical function available in all major DBMS

  • Deprecated/Recommended Alternatives: None

Error Handling:

  • Error: Returns NULL if input is NULL

  • Recommendation: Use NULLIF or CASE to handle negative inputs

Supported Databases:

DBMS
Function / Syntax
Example
Behavior with NULL
MySQL

SQRT(X)

SQRT(16)

Returns NULL

PostgreSQL

SQRT(number)

SQRT(16)

Returns NULL

SQL Server

SQRT(float_expression)

SQRT(16)

Returns NULL

SQLite

SQRT(X)

SQRT(16)

Returns NULL

BigQuery

SQRT(X)

SQRT(16)

Returns NULL

Snowflake

SQRT(number)

SQRT(16)

Returns NULL

Athena

SQRT(number)

SQRT(16)

Returns NULL

Related Functions:

SIGN()

Returns the sign of a number: -1 for negative numbers, 0 for zero, and 1 for positive numbers.

Returns the sign of a number: -1 for negative numbers, 0 for zero, and 1 for positive numbers.

Returns the sign of a number: -1 for negative numbers, 0 for zero, and 1 for positive numbers.

Returns the sign of a number: -1 for negative numbers, 0 for zero, and 1 for positive numbers.

RAND()/RANDOM()

Generates a random number. Without parameters, returns a floating-point number between 0 (inclusive) and 1 (exclusive). Some DBMS allow seeding for reproducible results.

Generates a random number. Without parameters, returns a floating-point number between 0 (inclusive) and 1 (exclusive). Some DBMS allow seeding for reproducible results.

Generates a random number. Without parameters, returns a floating-point number between 0 (inclusive) and 1 (exclusive). Some DBMS allow seeding for reproducible results.

Generates a random number. Without parameters, returns a floating-point number between 0 (inclusive) and 1 (exclusive). Some DBMS allow seeding for reproducible results.

MOD()

Returns the remainder after dividing one number by another. Also available as % operator in many databases.

Returns the remainder after dividing one number by another. Also available as % operator in many databases.

Returns the remainder after dividing one number by another. Also available as % operator in many databases.

Returns the remainder after dividing one number by another. Also available as % operator in many databases.

SQRT()

Returns the square root of a non-negative number. The square root is the value that, when multiplied by itself, gives the number.

Returns the square root of a non-negative number. The square root is the value that, when multiplied by itself, gives the number.

Returns the square root of a non-negative number. The square root is the value that, when multiplied by itself, gives the number.

Returns the square root of a non-negative number. The square root is the value that, when multiplied by itself, gives the number.

FLOOR()

Rounds a number down to the nearest integer or to a specified decimal place. Always rounds toward negative infinity.

Rounds a number down to the nearest integer or to a specified decimal place. Always rounds toward negative infinity.

Rounds a number down to the nearest integer or to a specified decimal place. Always rounds toward negative infinity.

Rounds a number down to the nearest integer or to a specified decimal place. Always rounds toward negative infinity.

CEIL()/CEILING()

Rounds a number up to the nearest integer or to a specified decimal place. Always rounds away from zero for positive numbers and toward zero for negative numbers.

Rounds a number up to the nearest integer or to a specified decimal place. Always rounds away from zero for positive numbers and toward zero for negative numbers.

Rounds a number up to the nearest integer or to a specified decimal place. Always rounds away from zero for positive numbers and toward zero for negative numbers.

Rounds a number up to the nearest integer or to a specified decimal place. Always rounds away from zero for positive numbers and toward zero for negative numbers.

ROUND()

Rounds a number to a specified number of decimal places or to the nearest integer.

Rounds a number to a specified number of decimal places or to the nearest integer.

Rounds a number to a specified number of decimal places or to the nearest integer.

Rounds a number to a specified number of decimal places or to the nearest integer.

One place for all your queries,
directly on your SQL editor