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
- Input number, e.g. 3*3 + 4*4 = 25
- Is the input negative? Yes: Return NULL or raise an error (depends on database). No: Continue.
- Find the value that squared gives the input
- Return 5
Syntax
SQRT(number)Example
SELECT SQRT(144);
-- 12Key points
- Returns NULL or an error for negative input depending on the database
- Equivalent to POWER(x, 0.5)
- Distance on a flat grid: SQRT(dx * dx + dy * dy)
- Standard deviation = SQRT(variance), though most databases offer STDDEV
- Usually returns a floating-point number — ROUND it for display