Pivot the Occupation column in OCCUPATIONS so that each Name is sorted alphabetically

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 DoctorProfessorSinger, and Actor, respectively.

HackerRank: https://www.hackerrank.com/challenges/occupations/problem

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

Input Format

The OCCUPATIONS table is described as follows:

Pivot the Occupation column in OCCUPATIONS so that each Name is sorted alphabetically

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

Sample Input

Pivot the Occupation column in OCCUPATIONS so that each Name is sorted alphabetically

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

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')
Pivot the Occupation column in OCCUPATIONS so that each Name is sorted alphabetically

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

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *