Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
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.

Practice SQL Exercises

Want to improve the above article? Contribute your Notes/Comments/Examples through Disqus.

Previous: MOD
Next: SQRT



SQL: Tips of the Day

SQL Server SELECT into existing table.

INSERT INTO dbo.TABLETWO
SELECT col1, col2
  FROM dbo.TABLEONE
 WHERE col3 LIKE @search_key

This assumes there's only two columns in dbo.TABLETWO - you need to specify the columns otherwise:

INSERT INTO dbo.TABLETWO
  (col1, col2)
SELECT col1, col2
  FROM dbo.TABLEONE
 WHERE col3 LIKE @search_key

Database: SQL Server

Ref: https://bit.ly/3y6tpA3