POWER() Function in MySQL
Last Updated :
01 Oct, 2020
POWER() function in MySQL is used to find the value of a number raised to the power of another number. It Returns the value of X raised to the power of Y.
Syntax :
POWER(X, Y)
Parameter : This method accepts two parameter which are described below :
- X : It specifies the base number.
- Y : It specifies the exponent number.
Returns : It returns the value of X raised to the power of Y.
Example-1 : Finding Power value when both base and exponent is positive using POWER() function.
SELECT POWER( 5, 4) AS Power_Value ;
Output :
Example-2 : Finding Power value when base and is positive but exponent is negative using POWER() function.
SELECT POWER( 2, -4) AS Power_Value ;
Output :
Example-3 : Finding Power value when base and is negative but exponent is positive using POWER() function.
SELECT POWER( -3, 3) AS Power_Value ;
Output :
Example-4 : Finding Power value when both base and exponent is negative using POWER() function.
SELECT POWER( -3, -4) AS Power_Value ;
Output :
Power_Value |
0.012345679012345678 |
Example-5 : The POWER function can also be used to find the power value between column data. To demonstrate create a table named.
Triangle.
CREATE TABLE Triangle(
Type VARCHAR(25) NOT NULL,
NoOfSides INT NOT NULL,
Base INT NOT NULL,
Height INT NOT NULL
);
Now inserting some data to the Triangle table :
INSERT INTO
Triangle(Type, NoOfSides, Base, Height )
VALUES
('Right-angled Triangle', 3, 4, 3 ),
('Right-angled Triangle', 3, 2, 5 ),
('Right-angled Triangle', 3, 1, 7 ),
('Right-angled Triangle', 3, 7, 9 ),
('Right-angled Triangle', 3, 4, 6 ),
('Right-angled Triangle', 3, 8, 3 ),
('Right-angled Triangle', 3, 10, 10 ) ;
Showing all data in Triangle Table –
Select * from Triangle ;
Type |
NoOfSides |
Base |
Height |
Right-angled Triangle |
3 |
4 |
3 |
Right-angled Triangle |
3 |
2 |
5 |
Right-angled Triangle |
3 |
1 |
7 |
Right-angled Triangle |
3 |
7 |
9 |
Right-angled Triangle |
3 |
4 |
6 |
Right-angled Triangle |
3 |
8 |
3 |
Right-angled Triangle |
3 |
10 |
10 |
Now, we are going to find the hypotenuse and area for each Right-angled Triangle.
SELECT
*,
sqrt(POWER(Base, 2) + POWER(Height, 2)) AS Hypotenuse,
0.5 * Base * Height as Area
FROM Triangle;
Output :
Type |
NoOfSides |
Base |
Height |
Hypotenuse |
Area |
Right-angled Triangle |
3 |
4 |
3 |
5 |
6.0 |
Right-angled Triangle |
3 |
2 |
5 |
5.385164807134504 |
5.0 |
Right-angled Triangle |
3 |
1 |
7 |
7.0710678118654755 |
3.5 |
Right-angled Triangle |
3 |
7 |
9 |
11.40175425099138 |
31.5 |
Right-angled Triangle |
3 |
4 |
6 |
7.211102550927978 |
12.0 |
Right-angled Triangle |
3 |
8 |
3 |
8.54400374531753 |
12.0 |
Right-angled Triangle |
3 |
10 |
10 |
14.142135623730951 |
50.0 |
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...