// math reference

SQL SQUARE function

Returns the square of a number as a float: SQUARE(5) is 25.0, the same value as x * x or POWER(x, 2).

Category
Math
Returns
float
In this category
25 of 82

Syntax

SQUARE(number)

Engine support

EngineSupportNotes
SQL Server Yes Supported under this name.
PostgreSQL Not built in No SQUARE; x * x or power(x, 2)
MySQL Not built in No SQUARE; x * x or POWER(x, 2)
SQLite Not built in No SQUARE; x * x
Oracle Not built in No SQUARE; x * x or POWER(x, 2)

Examples

SQL Server

SELECT SQUARE(5) AS twenty_five;  -- 25.0 (float)

SQL Server

SELECT SQRT(SQUARE(x1 - x2) + SQUARE(y1 - y2)) AS distance
FROM points;

Good to know

  • SQUARE converts its argument to float: SQUARE(5) is 25.0 and SQUARE of a DECIMAL is a float too. For exact money math, write x * x, which keeps the decimal type.
  • POWER(x, 2) on SQL Server returns the type of x instead, so POWER(5, 2) is the INT 25 while SQUARE(5) is the FLOAT 25.0.
  • Because the result is float, SQUARE does not overflow INT the way x * x does: SQUARE(100000) is the float 10000000000, while 100000 * 100000 raises an arithmetic overflow. The price is float precision past 15 digits.

Questions about SQUARE

What is the difference between SQUARE and POWER(x, 2)?

The value is the same, the type is not. SQUARE always returns float; SQL Server's POWER(x, 2) returns the type of x. POWER is also the portable spelling across all five engines.

How do I square a number outside SQL Server?

Multiply it by itself: x * x works on every engine and keeps integer and decimal types exact. POWER(x, 2) also works but returns double on MySQL, SQLite and PostgreSQL for integer input.

Does SQUARE overflow on big integers?

Not at INT range, since the result is float: SQUARE(100000) returns the float 10000000000. x * x on two INTs overflows at 2147483647, so cast to BIGINT there. SQUARE only fails past the float limit near 1.8e308.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.