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 |
Similar Reads
POW() Function in MySQL
POW() function : This function in MySQL is used to return a results after raising a specified exponent number to a specified base number. For example if the base is 5 and exponent is 2, this will return a result of 25. Syntax : SELECT POW(x, y); Parameter : This method accepts two parameters as give
1 min read
ORD() Function in MySQL
ORD() function in MySQL is used to find the code of the leftmost character in a string . If the leftmost character is not a multibyte character, it returns ASCII value. And if the leftmost character of the string str is a multibyte character, ORD returns the code for that character, calculated from
3 min read
YEAR() Function in MySQL
YEAR() function in MySQL is used to find year from the given date. If the date is NULL, the YEAR() function will return NULL. Otherwise, it returns value range from 1000 to 9999. Syntax : YEAR(date) Parameter : This method accepts one parameter as mentioned above and described below : date : The dat
3 min read
PI() function in MySQL
PI() function in MySQL is used to return the Pi value. The default number of decimal places displayed is seven, but MySQL uses the full double-precision value internally. Syntax : PI() Parameter : This method does not accept any parameter. Returns : It returns the Pi value i.e. 3.141593. Example-1 :
2 min read
QUOTE () function in MySQL
QUOTE() : This function in MySQL is used to return a result that can be used as a properly escaped data value in an SQL statement. The string is returned enclosed by single quotation marks and with each instance of backslash (\), single quote ('), ASCII NULL, and Control+Z preceded by a backslash. I
2 min read
SPACE() Function in MySQL
SPACE() function in MySQL is used to return a string consisting of specified empty space characters. Syntax : SPACE(num) Parameter : This method accepts one parameter as mentioned above and described below : num : It is an integer which indicates how many spaces are being contained. Returns : It ret
1 min read
OCT() function in MySQL
OCT() function in MySQL is used to convert decimal number to octal. It returns equivalent octal value of a decimal number. Syntax : OCT(number) Parameter : This method accepts only one parameter. number : The decimal number which we want to convert. Returns : It returns octal value of a decimal numb
2 min read
BIT_OR() Function in MySQL
BIT_OR() function in MySQL is used to return the bitwise OR of all bits in a given expression. It first converts all decimal values into binary values, and then perform bitwise or operation on those binary values. Syntax : BIT_OR(expr) Parameter : This method accepts only one parameter. expr - Input
4 min read
REPEAT() function in MySQL
REPEAT() : This function in MySQL is used to repeat a string a specified number of times. Syntax : REPEAT(str, count) Parameters : This method accepts two parameter. str -Input String which we want to repeat. count -It will describe that how many times to repeat the string. Returns : It returns a re
2 min read
PLSQL | POWER Function
The POWER function is an inbuilt function in PLSQL which is used to return the calculated value when a raised to the bth power. If a is negative number, then b must be an integer. Syntax: POWER(a, b) Parameters Used: This function accepts two parameters a and b. If a is negative number, then b must
2 min read