The POWER() function in SQL is a mathematical function used to raise a number to the power of another number. It returns the result of the specified base number raised to the specified exponent.
- Useful for mathematical and numerical calculations.
- Used with integer and decimal values.
Syntax
POWER(number, exponent)- number: The base number that you want to raise to a power.
- exponent: The power to which the number is raised.
Note: The POWER() function is supported by MySQL, SQL Server, PostgreSQL and Oracle. SQLite does not provide POWER() as a standard built-in function.
Examples
Below are some examples of the POWER() function to understand how it is used in different situations.
Example 1: Calculate the Square of a Number
In this example, we will calculate the square of 5.
Query:
SELECT POWER(5, 2) AS square;Output:
Example 2: Calculate the Cube of a Number
In this example, we will calculate the cube of 4.
Query:
SELECT POWER(4, 3) AS cube;Output:
Example 3: Use POWER() on a Table Column
Let us create a products table containing product prices.
Now, we can use the POWER() function to calculate the square of each product price.
Query:
SELECT product_name,price,POWER(price, 2) AS price_squareFROM products;
Output:
- Calculates the square of each value in the price column.
- Returns the calculated values as price_square.