MySQL - POWER() Function



The MySQL POWER() function is a synonym for POW(), used to calculate the power of a number.

This function accepts two non-negative numbers as arguments, one number is considered a base while the other is the exponent. The return value will be the power of first argument passed.

Syntax

Following is the syntax of MySQL POWER() function −

POWER(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 POWER() function to calculate 144 raised to the power of 3 −

SELECT POWER(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 value −

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

Output

This will produce the following result −

Result
125

Example

We can pass negative values as arguments to this method −

SELECT POWER(7, -3) As Result;

Output

This will produce the following result −

Result
0.0029154518950437317

Example

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

SELECT POWER(9, 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 POWER() 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