MySQL - POW() Function



The MySQL POW() function is used to calculate the power of a number. It accepts two arguments: the base and the exponent. The function returns the result of raising the base to the power of the exponent.

Syntax

Following is the syntax of MySQL POW() function −

POW(X,Y);

Parameters

This function takes a base value (x) and an exponent value (y) as parameters.

Return Value

This function returns the value of x raised to the power of y.

Example

In the following example, we are using the MySQL POW() function to calculate the result of raising 144 to the power of 3 −

SELECT POW(144, 3) As Result;

Output

The output for the query above is produced as given below −

Result
2985984

Example

We can also pass the arguments of this function as a string values −

SELECT POW('5', '3') As Result;

Output

This will produce the following result −

Result
125

Example

We can pass negative values as arguments to this method.

Here, we are calculating the result of raising 7 to the power of negative integer "-3" −

SELECT POW(7, -3) As Result;

This will produce the following result −

Result
0.0029154518950437317

Now, we are calculating the result of raising "-25" to the power of "-3" −

SELECT POW(-25, -3) As Result;

Following is the output −

Result
-0.000064

Example

If you raise any value to the power of 0 the result will be 1

SELECT POW(7, 0) As Result;

Output

Following is the output −

Result
1

Example

We can use POWER function to find out squares of various records as well. To do so, let us create a table named CUSTOMERS using the following query −

CREATE TABLE CUSTOMERS (
   ID INT AUTO_INCREMENT,
   NAME VARCHAR(20) NOT NULL,
   AGE INT NOT NULL,
   ADDRESS CHAR (25),
   SALARY DECIMAL (18, 2),
   PRIMARY KEY (ID)
);

The following query inserts 7 records into the above created table −

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES 
(1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ),
(2, 'Khilan', 25, 'Delhi', 1500.00 ),
(3, 'Kaushik', 23, 'Kota', 2000.00 ),
(4, 'Chaitali', 25, 'Mumbai', 6500.00 ),
(5, 'Hardik', 27, 'Bhopal', 8500.00 ),
(6, 'Komal', 22, 'Hyderabad', 4500.00 ),
(7, 'Muffy', 24, 'Indore', 10000.00 );

Execute the below query to display all the records present in the CUSTOMERS table −

Select * From CUSTOMERS;

Following is the CUSTOMERS table −

ID NAME AGE ADDRESS SALARY
1 Ramesh 32 Ahmedabad 2000.00
2 Khilan 25 Delhi 1500.00
3 Kaushik 23 Kota 2000.00
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 8500.00
6 Komal 22 Hyderabad 4500.00
7 Muffy 24 Indore 10000.00

Now, we are using the POW() function to calculate the square of values in "SALARY" column −

SELECT *, POW(SALARY, 2) FROM CUSTOMERS;

The output for the query above is produced as given below −

ID NAME AGE ADDRESS SALARY POW(SALARY, 2)
1 Ramesh 32 Ahmedabad 2000.00 4000000
2 Khilan 25 Delhi 1500.00 2250000
3 Kaushik 23 Kota 2000.00 4000000
4 Chaitali 25 Mumbai 6500.00 42250000
5 Hardik 27 Bhopal 8500.00 72250000
6 Komal 22 Hyderabad 4500.00 20250000
7 Muffy 24 Indore 10000.00 100000000
Advertisements