Square Function In Sql in SQL
The SQUARE function in SQL returns the square of a given number, i.e., the number multiplied by itself.
? Syntax
SQUARE(numeric_expression)numeric_expression: Any valid numeric type (int, float, decimal, etc.)
? Return Value
Returns the square of the input number.
Output is typically a float or numeric depending on the input.
? Basic Examples
SELECT SQUARE(5) AS SquareOf5; -- Returns 25SELECT SQUARE(-3.2) AS SquareOfNeg;-- Returns 10.24SELECT SQUARE(0) AS SquareOfZero; -- Returns 0? Use Cases
Mathematical calculations (e.g., variance, physics formulas).
Distance calculations using the Pythagorean theorem:
SELECT SQRT(SQUARE(x2 - x1) + SQUARE(y2 - y1)) AS Distance;Data analysis involving standard deviation or trends.
?? Notes
SQUAREis not the same asPOWER(number, 2)but achieves the same result and is more efficient for squaring.If input is NULL, output is also NULL.
? Bonus Example with Table Values
SELECT Value, SQUARE(Value) AS SquareValueFROM ( VALUES (1), (2), (3), (-4), (0)) AS SampleData(Value);Let me know if you'd like help using SQUARE in formulas like standard deviation, energy equations, or financial models.