Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Hierarchical Queries in Oracle SQL

Nivetha RamnathMay 14 2018 — edited May 14 2018

Hi,

I have a query which displays the hierarchical relation between department and employee.

WITH dept_emps AS

     (SELECT d.dept_id ID, d.dept_name nm, NULL emp_id

        FROM dept d

      UNION ALL

      SELECT e.dept_id ID, e.ename,

             e.empno emp_id

        FROM emp e)

SELECT   CASE

            WHEN emp_id IS NOT NULL

               THEN '    ' || nm

            ELSE nm

         END nm

    FROM dept_emps

ORDER BY ID, emp_id NULLS FIRST;

NM

SALES

    SMITH

    ADAMS

CD

    JONES

    SCOTT

DBA

    FORD

    CLARK

But my required output is:

Dept            Employee

SALES        SMITH

                    ADAMS

CD               JONES

                    SCOTT

DBA             FORD

                     CLARK

Can anyone please help how to achieve this?

Regards,

Nivetha Ramnath

This post has been answered by Solomon Yakobson on May 14 2018
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 11 2018
Added on May 14 2018
8 comments
238 views