EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Oracle Tutorial PIVOT in Oracle
 

PIVOT in Oracle

Priya Pedamkar
Article byPriya Pedamkar

Updated May 15, 2023

PIVOT in Oracle

 

 

What is an Oracle PIVOT Clause?

An Oracle PIVOTclauseis used to get a result in a matrix form. It allows us to write a query in cross-tabulation form. It also enables us to transpose rows into columns. It is introduced in Oracle 11g version and it supports 11g or later version. This is a very useful function for transposing rows into columns and aggregating data in the process. It is also called a CROSSTAB query. Multiple columns can be used in the PIVOT clause. If rows need to be converted into columns to split out a specific set of data points then the PIVOT clause can be used.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax and Parameters

The syntax of the pivot in oracle:

Syntax:

SELECT Columns FROM Table_Name
PIVOT [XML] (
Pivot_Clause
Pivot_For_Clause
Pivot_In_Clause
)   WHERE Conditions

Parameter:

  • Columns: Columns that needs to be displayed or fetched.
  • Table_Name: Table that can be used for data.
  • XML: It is used to convert pivoted data into xml. It is an optional keyword.
  • Pivot_Clause: It uses Oracle aggregate function on the column’s data to fill the pivoted column accordingly. Oracle aggregate functions are SUM,COUNT,MIN,MAX or AVG.
  • Pivot_For_Clause: In this clause, specify column will be grouped or pivoted.
  • Pivot_In_Clause: It is used to define a filter for the specified column in Pivot_For_Clause.
  • WHERE: This clause is used to filter the data. It’s optional.
Note: Subquery can be used instead of Table_Name. It’s not mandatory but it depends on the requirement. ORDER BY clause can be used for sorting the result but it could be the last clause. Subquery can be used for Pivot_In_Clause to get the column name.

How does PIVOT Clause Work in Oracle?

ThePIVOTclause is a key technique in many data warehouse applications. Its main functionality is to transpose rows into the column. It transforms multiple input rows into columns, aggregating data as part of the rotation process.

Implementing EXTRACT( ) Function

In this section we’ll see the implementation of Oracle PIVOT Clause and its behavior. For that, we will use the below sample table (Employee) with 14records to understand the Oracle PIVOT Clause behavior.

SELECT * FROM Employee;

Output:

PIVOT in Oracle-1.1

1. Oracle PIVOT Clause

SELECT * FROM (SELECT Name, Designation, Deptnumber, Salary FROM                  Employee) PIVOT (SUM (Salary) FORDeptnumber IN (10,20,30));

Output:

PIVOT in Oracle-1.2

In the above example, In the PIVOTclause SUM () function performs a summation of salary forDeptnumbrwhich is declared using Pivot FOR clause. In Pivot IN clause, department numbers have been declared which is pointing to the FOR clause Deptnumbr column and converts Deptnumbr column values into column names and displays the sum of salary Deptnumber wise.

2. PIVOT Column Aliasing or Alias Name

Pivot column aliasing is nothing but to provide a temporary name to the column or values.

  • In the Pivot_Clause, one or more columns can have an alias name.
  • In the Pivot_In_Clause, one or more values can have an alias name.
  • The final column name (after aliasing) will be:

Pivot_In_Clause Alias || ‘_’ || Pivot_Clause Alias
SELECT * FROM (SELECT Designation, Deptnumber, Salary FROM Employee)                       PIVOT (SUM (Salary) Dept_Sal FOR Deptnumber IN (10, 20, 30));

Output:

PIVOT in Oracle-1.3

In the above example, Dept_Sal is an Alias name of Pivot_Clause (SUM (Salary)). The alias name gets concatenated or added with Pivot_In_Clause using underscore ‘_’.

3. Multiple Columns in PIVOT Clause

In PIVOT clause, multiple columns can be used in Pivot_Clause.

SELECT * FROM (SELECT Designation, Deptnumber, Salary FROM Employee)          PIVOT (SUM (Salary) Dept_Sal, COUNT(DeptNumber) Dept_Count FOR                             Deptnumber IN (10, 20));

Output:

Output-1.4

In the above PIVOTClause, there are two columns (Salary & DeptNumber) are being used for aggregation in Pivot_Clause. The output showing summation of salary department wise and count of the employee Deptnumber wise who falls in the same department with the same designation and returns the result

4. Sub Query in Oracle PIVOT

SELECT * FROM (SELECT Designation, Deptnumber, Salary FROM Employee)    PIVOT (SUM(Salary) Dept_Sal, COUNT(DeptNumber) Dept_Count FOR                                   Deptnumber IN (select Deptnumber from employee));

Output:

Output-1.5

The above PIVOT Clause example returns an error “missing expression”.WHY?

Because that subquery return all department value means duplicate values that’s why in Pivot_In_Clause subquery is not allowed or not acceptable. If you declare, it will return an error.

But Sub Query can be used in Pivot_In_Clause, see the example below.

SELECT * FROM (SELECT Designation, Deptnumber, Salary FROM Employee)    PIVOT (SUM(Salary) Dept_SalFOR Deptnumber IN (select Deptno FROMEmp));

Output:

Output-1.6

In the above Pivot clause, used a subquery to populate department number in Pivot_In_Clause and it returns an output without any error. Because in the above example XML keyword is used which converts subquery returns values in a single XML string column.

Note: Subquery must return a list of unique value(s). In Subquery Group by clause won’t work for unique value instead of XML keyword.

Conclusion

Oracle PIVOTclause is a very useful clause that adds more practical functionality in the field of data analysis or crosstab reporting. Pivot clause reduced writing of convoluted non-intuitive or lengthy code. It is a very efficient way to transpose the data.

Recommended Articles

This is a guide to PIVOT in Oracle. Here we also discuss the introduction and how does pivot clause work in oracle? along with different examples and its code implementation. you may also have a look at the following articles to learn more –

  1. Oracle LOCK TABLE
  2. IF THEN ELSE in Oracle
  3. Oracle REGEXP_REPLACE
  4. Oracle CARDINALITY

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW