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
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
- SQL Exercises, Practice, Solution
- SQL Retrieve data from tables [33 Exercises]
- SQL Boolean and Relational operators [12 Exercises]
- SQL Wildcard and Special operators [22 Exercises]
- SQL Aggregate Functions [25 Exercises]
- SQL Formatting query output [10 Exercises]
- SQL Quering on Multiple Tables [8 Exercises]
- FILTERING and SORTING on HR Database [38 Exercises]
- SQL JOINS
- SQL SUBQUERIES
- SQL Union[9 Exercises]
- SQL View[16 Exercises]
- SQL User Account Management [16 Exercise]
- Movie Database
- BASIC queries on movie Database [10 Exercises]
- SUBQUERIES on movie Database [16 Exercises]
- JOINS on movie Database [24 Exercises]
- Soccer Database
- Introduction
- BASIC queries on soccer Database [29 Exercises]
- SUBQUERIES on soccer Database [33 Exercises]
- Hospital Database
- Employee Database
- More to come!
Want to improve the above article? Contribute your Notes/Comments/Examples through Disqus.
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
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework