DUET PG Score Card 2021
NHAI Recruitment 2021
Bihar Board 12th Dummy Admit Card 2022
RBI Assistant Notification 2021
Jee Advanced 2021 Result
NIACL Assistant Recruitment 2021
SBI PO Online Form 2021

3. Pivot the Occupation column in OCCUPATIONS so that each Name is sorted alphabetically and displayed underneath its corresponding Occupation in ORACLE SQL

Que: 3. Pivot the Occupation column in OCCUPATIONS so that each Name is sorted alphabetically and displayed underneath its corresponding Occupation. The output column headers should be Doctor, Professor, Singer, and Actor, respectively.

Note: Print NULL when there are no more names corresponding to an occupation.

Pivot the Occupation column in OCCUPATIONS

Input Format: The OCCUPATIONS table is described as follows:

1443816414 2A465532E7 1 Speedjobalert

Occupation will only contain one of the following values: DoctorProfessorSinger, or Actor.

Sample Input

1443817648 1B2B8Add45 2 Speedjobalert

Sample Output

Jenny    Ashley     Meera  Jane
Samantha Christeen  Priya  Julia
NULL     Ketty      NULL   Maria
Que: 3. Pivot The Occupation Column In Occupations So That Each Name Is Sorted
Alphabetically And Displayed Underneath Its Corresponding Occupation. The Output Column
Headers Should Be Doctor, Professor, Singer, And Actor, Respectively. [10]
Note: Print Null When There Are No More Names Corresponding To An Occupation.

Explanation

The first column is an alphabetically ordered list of Doctor names.
The second column is an alphabetically ordered list of Professor names.
The third column is an alphabetically ordered list of Singer names.
The fourth column is an alphabetically ordered list of Actor names.
The empty cell data for columns with less than the maximum number of names per occupation (in this case, the Professor and Actor columns) are filled with NULL values.

Solution of Pivot the Occupation column in OCCUPATIONS

Solution

create table occupations(name varchar(50), occupation varchar(50))
INSERT INTO occupations(name, occupation) values('Samantha','Doctor')
INSERT INTO occupations(name, occupation) values('Julia','Actor')
INSERT INTO occupations(name, occupation) values('Maria','Actor')
INSERT INTO occupations(name, occupation) values('Meera','Singer')
INSERT INTO occupations(name, occupation) values('Ashely','Professor')
INSERT INTO occupations(name, occupation) values('Ketty','Professor')
INSERT INTO occupations(name, occupation) values('Christeen','Professor')
INSERT INTO occupations(name, occupation) values('Jane','Actor')
INSERT INTO occupations(name, occupation) values('Jenny','Doctor')
INSERT INTO occupations(name, occupation) values('Priya','Singer')
Image 1 Speedjobalert
select doctor,professor,singer,actor from
(
select * from (select name, occupation, (ROW_NUMBER() OVER 
(PARTITION BY occupation ORDER BY name)) as row_num 
from occupations order by name asc) pivot ( min(name) for 
occupation in ('Doctor' as doctor,'Professor' as professor,'Singer' as singer,'Actor' as actor)) 
order by row_num)
Que: 3. Pivot The Occupation Column In Occupations So That Each Name Is Sorted Alphabetically And Displayed Underneath Its Corresponding Occupation In Oracle Sql

ORACLE Live SQL: https://livesql.oracle.com/apex/f?p=590:1000

Download Source Code: 👇

Candidates having any kind of query so he/she can ask in our comment section. Our Panel will b reverting back as soon as possible. Thank You. For more details visit our website (https://speedjobalert.com/)

2 thoughts on “3. Pivot the Occupation column in OCCUPATIONS so that each Name is sorted alphabetically and displayed underneath its corresponding Occupation in ORACLE SQL”

  1. select doctor,professor,singer,actor from
    (
    select * from (select name, occupation, (ROW_NUMBER() OVER
    (PARTITION BY occupation ORDER BY name)) as row_num
    from occupations order by name asc) pivot ( min(name) for
    occupation in (‘Doctor’ as doctor,’Professor’ as professor,’Singer’ as singer,’Actor’ as actor))
    order by row_num)

    THE ADVANCE PROBLEM FROM OCCUPATION IN SQL HACKER RANK IS NOT GIVING OUTPUT
    SHOWING ERROR AFTER PIVOT SO PLEASE HELP

    Reply
    • — change ‘Doctor’ single quotation to ‘Doctor’

      select doctor,professor,singer,actor from
      (
      select * from (select name, occupation, (ROW_NUMBER() OVER
      (PARTITION BY occupation ORDER BY name)) as row_num
      from occupations order by name asc) pivot ( min(name) for
      occupation in (‘Doctor’ as doctor,’Professor’ as professor,’Singer’ as singer,’Actor’ as actor))
      order by row_num)

      Reply

Leave a Comment