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:


-- This SQL statement calculates the result of raising 2 to the power of 3 and selects the result from the 'dual' table.
SELECT POWER(2,3) 
-- SELECT statement retrieves data from the database
-- POWER() function calculates the result of raising a number to a specified power
-- In this case, POWER(2,3) calculates 2 raised to the power of 3
FROM dual;
-- Specifies the 'dual' table, a special one-row, one-column table present in Oracle database
-- The 'dual' table is often used for performing calculations or returning single results in SQL queries

Explanation:

  • This SQL query is straightforward, as it's only a single statement.

  • The purpose of this query is to calculate the result of raising 2 to the power of 3.

  • POWER() is a mathematical function that calculates the result of raising a number to a specified power.

  • In this case, POWER(2,3) will return the result of 2 raised to the power of 3, which is 8.

  • The result of the calculation, which is 8 in this case, will be selected as the output of the query.

  • The 'dual' table is used here because it's a convenient way to execute single-row queries in Oracle SQL without needing to specify an actual table with data.

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 :


-- This SQL statement selects customer name, customer country, grade, and the result of raising the grade to the power of 3 for each row in the 'customer' table.
SELECT cust_name, cust_country, grade, POWER(grade,3) 
-- SELECT statement retrieves data from the database
-- cust_name, cust_country, and grade are columns selected for output
-- POWER() function calculates the result of raising a number to a specified power
-- In this case, POWER(grade,3) calculates the result of raising the grade to the power of 3
FROM customer;
-- Specifies the 'customer' table from which the data is being retrieved

Explanation:

  • This SQL query is designed to retrieve specific information from the 'customer' table.

  • The SELECT statement specifies the columns that will be included in the output: cust_name, cust_country, grade, and the result of raising the grade to the power of 3.

  • POWER() is a mathematical function that calculates the result of raising a number to a specified power.

  • In this case, POWER(grade,3) will return the result of raising the 'grade' column value to the power of 3 for each row in the 'customer' table.

  • The query is executed against the 'customer' table to retrieve the required data.

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:


-- This SQL statement selects customer name, customer country, grade, and the result of raising the grade to the power of 3 for each row in the 'customer' table where the customer country is 'UK'.
SELECT cust_name, cust_country, grade, POWER(grade,3) 
-- SELECT statement retrieves data from the database
-- cust_name, cust_country, and grade are columns selected for output
-- POWER() function calculates the result of raising a number to a specified power
-- In this case, POWER(grade,3) calculates the result of raising the grade to the power of 3
FROM customer 
-- Specifies the 'customer' table from which the data is being retrieved
WHERE cust_country = 'UK';
-- WHERE clause filters the rows based on a condition
-- It selects only those rows where the customer country is 'UK'

Explanation:

  • This SQL query is designed to retrieve specific information from the 'customer' table for customers located in the UK.

  • The SELECT statement specifies the columns that will be included in the output: cust_name, cust_country, grade, and the result of raising the grade to the power of 3.

  • POWER() is a mathematical function that calculates the result of raising a number to a specified power.

  • In this case, POWER(grade,3) will return the result of raising the 'grade' column value to the power of 3 for each row in the 'customer' table where the customer country is 'UK'.

  • The WHERE clause filters the rows from the 'customer' table based on the condition that the customer country is 'UK'.

  • The query is executed against the 'customer' table to retrieve the required data.

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:


-- This SQL statement selects distinct results of raising the grade to the power of 3 for each row in the 'customer' table.
SELECT DISTINCT(POWER(grade,3)) 
-- SELECT statement retrieves data from the database
-- DISTINCT keyword ensures that only unique values are returned
-- POWER() function calculates the result of raising a number to a specified power
-- In this case, POWER(grade,3) calculates the result of raising the grade to the power of 3
FROM customer;
-- Specifies the 'customer' table from which the data is being retrieved

Explanation:

  • This SQL query is designed to retrieve distinct results of raising the grade to the power of 3 from the 'customer' table.

  • The SELECT statement specifies the expression POWER(grade,3) which calculates the result of raising the 'grade' column value to the power of 3.

  • The DISTINCT keyword ensures that only unique values of the calculated expression are returned, preventing duplicates.

  • The query is executed against the 'customer' table to retrieve the required data.

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 :


-- This SQL statement calculates the result of raising the grade to the power of 3 for each row in the 'customer' table where the customer country is 'UK', and groups the results by the calculated power values.
SELECT POWER(grade,3) 
-- SELECT statement retrieves data from the database
-- POWER() function calculates the result of raising a number to a specified power
-- In this case, POWER(grade,3) calculates the result of raising the grade to the power of 3
FROM customer 
-- Specifies the 'customer' table from which the data is being retrieved
WHERE cust_country = 'UK' 
-- WHERE clause filters the rows based on a condition
-- It selects only those rows where the customer country is 'UK'
GROUP BY POWER(grade,3);
-- GROUP BY clause groups the results based on a specified expression
-- In this case, it groups the results by the calculated power values of grade raised to the power of 3

Explanation:

  • This SQL query is designed to calculate the result of raising the grade to the power of 3 for each customer located in the UK and group the results by the calculated power values.

  • The SELECT statement specifies the expression POWER(grade,3) which calculates the result of raising the 'grade' column value to the power of 3.

  • The WHERE clause filters the rows from the 'customer' table based on the condition that the customer country is 'UK'.

  • The GROUP BY clause groups the results based on the calculated power values of grade raised to the power of 3. This ensures that rows with the same power value are grouped together.

  • The query is executed against the 'customer' table to retrieve the required data.

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.