w3resource

SQL POWER() function

POWER() function

SQL POWER() function returns the value of a number raised to another, where both of the numbers are passed as arguments. The SQL DISTINCT command along with the SQL POWER() function can be used to retrieve only unique data depending on a specified expression.

Syntax:

POWER( base, exponent )

Parameters:

Name Description
base A number.
exponent A number.

PostgreSQL, MySQL, SQL Server and Oracle

All of above platforms support the SQL syntax of POWER().

SQL POWER() function: Pictorial presentation

SQL POWER() function Example:

To get the power of 2 raised by 3 from the DUAL table, the following SQL statement can be used:

SELECT POWER(2,3) 
FROM dual;

Output:

POWER(2,3)
----------
         8

SQL POWER() function using column

Sample table: customer


To get data of 'cust_name', 'cust_country', 'grade' and power of 'grade' raised by 3 from the 'customer' table, the following SQL statement can be used :

SELECT cust_name,cust_country,grade, POWER(grade,3) 
FROM customer;

Output:

CUST_NAME                                CUST_COUNTRY              GRADE POWER(GRADE,3)
---------------------------------------- -------------------- ---------- --------------
Holmes                                   UK                            2              8
Micheal                                  USA                           2              8
Albert                                   USA                           3             27
Ravindran                                India                         2              8
Cook                                     UK                            2              8
Stuart                                   UK                            1              1
Bolt                                     USA                           3             27
Fleming                                  Australia                     2              8
Jacks                                    Australia                     1              1
Yearannaidu                              India                         1              1
Sasikant                                 India                         1              1
Ramanathan                               India                         1              1
Avinash                                  India                         2              8
Winston                                  Australia                     1              1
Karl                                     UK                            0              0
Shilton                                  Canada                        1              1
Charles                                  UK                            3             27
Srinivas                                 India                         2              8
Steven                                   USA                           1              1
Karolina                                 Canada                        1              1
Martin                                   Canada                        2              8
Ramesh                                   India                         3             27
Rangarappa                               India                         2              8
Venkatpati                               India                         2              8
Sundariya                                India                         3             27

SQL POWER() function with where

Sample table: customer


To get data of 'cust_name', 'cust_country', 'grade' and the power of 'grade' raised by 3 from the 'customer' table with the following condition -

1. 'cust_country' must be 'UK'

the following SQL statement can be used:

SELECT cust_name,cust_country, grade, 
POWER(grade,3) 
FROM customer 
WHERE cust_country = 'UK';

Output:

CUST_NAME                                CUST_COUNTRY              GRADE POWER(GRADE,3)
---------------------------------------- -------------------- ---------- --------------
Charles                                  UK                            3             27
Holmes                                   UK                            2              8
Cook                                     UK                            2              8
Stuart                                   UK                            1              1
Karl                                     UK                            0              0

SQL POWER() function with distinct

Sample table: customer


To get the unique power of 'grade' raised by 3 from the 'customer' table, the following SQL statement can be used:

SELECT DISTINCT(POWER(grade,3)) 
FROM customer;

Output:

(POWER(GRADE,3))
----------------
               1
               8
              27
               0

SQL POWER() function with group by

Sample table: customer


To get the power of 'grade' raised by 3 from the 'customer' table with following conditions -

1. 'cust_country' must be 'UK',

2. each unique power of 'grade' raised by 3 should be in a group,

the following SQL statement can be used :

SELECT POWER(grade,3) 
FROM customer 
WHERE cust_country = 'UK' 
GROUP BY POWER(grade,3);

Output:

POWER(GRADE,3)
--------------
             1
             8
            27
             0

Note: Outputs of the said SQL statement shown here is taken by using Oracle Database 10g Express Edition.

Here is a slide presentation which covers the SQL arithmetic functions.

Check out our 1000+ SQL Exercises with solution and explanation to improve your skills.

Previous: MOD
Next: SQRT



Follow us on Facebook and Twitter for latest update.