Skip to main content
Log inGet a demo
Back to SQL Dictionary
Aggregate functions

SQL SQRT

What is SQL SQRT?

The SQL SQRT function, which stands for "square root," is used to calculate the square root of a numeric value. It returns the non-negative square root of a given number, which is a value that, when multiplied by itself, equals the original number.

When you would use it

You would use the SQL SQRT function when you need to calculate the square root of a numeric value in your SQL query. It is commonly used in various mathematical and scientific applications, such as calculating distances, handling geometric data, or working with numerical data that requires square root calculations.

Syntax

The syntax for using the SQRT function is as follows:

SELECT SQRT(numeric_value) FROM table_name WHERE condition;
  • numeric_value: The numeric value for which you want to calculate the square root.
  • table_name: The name of the table containing the data (optional).
  • condition (optional): An optional condition to filter the rows (if using data from a table).

Parameter values

  • numeric_value: The numeric value for which you want to calculate the square root.
  • table_name: The name of the table where the data is stored (optional).
  • condition: A condition that filters the rows (if you are using data from a table).

Example query

Suppose we have a table named "distances" with columns "distance_id" and "distance_squared." The "distance_squared" column contains the squared distances between two points. We want to calculate the square root of these squared distances to obtain the actual distances:

SELECT distance_id, SQRT(distance_squared) AS actual_distance
FROM distances
-- Comment the line below to show it doesn't affect the query.
-- WHERE location = 'City Center';

In the above query, we use the SQRT function to calculate the actual distances from the squared distances.

Example table response

Assuming the "distances" table contains the following data:

| distance_id | distance_squared |
|------------ | ---------------- |
| 1          | 25               |
| 2          | 100              |
| 3          | 144              |
| 4          | 36               |

The query mentioned earlier would return the following result:

| distance_id | actual_distance |
|------------ | --------------- |
| 1          | 5.00            |
| 2          | 10.00           |
| 3          | 12.00           |
| 4          | 6.00            |

This result shows the actual distances calculated by taking the square root of the squared distances.

Use cases

  • Calculating geometric data, such as distances, coordinates, or dimensions.
  • Handling numerical data that requires square root calculations.
  • Scientific and mathematical calculations that involve square roots.

SQL languages this is available for

The SQL SQRT function is a standard SQL feature and is available in most relational database management systems (RDBMS) that support SQL. This includes popular RDBMS like MySQL, PostgreSQL, Oracle, SQL Server, and SQLite. The specific syntax and behavior may vary slightly between database systems, but the fundamental functionality remains the same.

Related

SQL POWER

SQL BIN

SQL LIKE

Ready to put your SQL knowledge to work?

Practice writing SQL to call data from the warehouse and sync it into Google Sheets in this 5 minute interactive demo.

Hightouch Audiences user interface.