SQRT()

SQL · Math Functions

Theory

SQRT returns the square root of a number: the value that, multiplied by itself, gives the input. SQRT(144) is 12, SQRT(2) is about 1.4142 and SQRT(0) is 0. The input must be zero or positive, because negative numbers have no real square root; depending on the database you get NULL (SQLite, MySQL) or an error (PostgreSQL, SQL Server).

SQRT shows up in two common places. In geometry, the straight-line distance between two points is SQRT((x2 - x1)^2 + (y2 - y1)^2), so a delivery point 3 km east and 4 km north of a store is SQRT(9 + 16) = 5 km away. In statistics, standard deviation is the square root of variance; most databases already provide STDDEV, but you may compute it by hand on platforms that lack it. SQRT is supported everywhere, including SQLite builds with math functions, and SQRT(x) is the same as POWER(x, 0.5).

Why we use it

Distances and spreads are defined using squares, and SQRT brings the result back to the original units (km, rupees). Without it you would compare squared values that are hard to interpret.

When to use it

Use SQRT for straight-line distances on a flat grid, standard deviation and other formulas that square values. For real map distances on the Earth's surface, use a geospatial function or the haversine formula instead.

How it works

  1. Input number, e.g. 3*3 + 4*4 = 25
  2. Is the input negative? Yes: Return NULL or raise an error (depends on database). No: Continue.
  3. Find the value that squared gives the input
  4. Return 5

Syntax

SQRT(number)

Example

SELECT SQRT(144);
-- 12

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic