Here's an example (based on Scott's schema). Employees, sorted by MGR (manager):
Code:
SQL> select mgr, empno, ename
2 from emp
3 order by mgr, empno;
MGR EMPNO ENAME
---------- ---------- ----------
7566 7788 SCOTT
7566 7902 FORD
-> 7698 7499 ALLEN
-> 7698 7521 WARD
-> 7698 7654 MARTIN
-> 7698 7844 TURNER
-> 7698 7900 JAMES
7782 7934 MILLER
7788 7876 ADAMS
7839 7566 JONES
7839 -> 7698 BLAKE
7839 7782 CLARK
7902 7369 SMITH
7839 KING
14 rows selected.
SQL>
Now, suppose that you enter EMPNO = 7844 as a parameter. That is TURNER. His manager is 7698 (BLAKE). So, as a result, you want to fetch all emplyoees whose manager is BLAKE, as well as BLAKE himself.
OK, here's one option: first, find the manager and then use that value in order to retrieve desired records:
Code:
SQL> with manager as
2 (select mgr
3 from emp
4 where empno = &supplied_empno
5 )
6 select e.empno, e.ename
7 from emp e, manager m
8 where e.mgr = m.mgr --> employees whose manager is BLAKE
9 or e.empno = m.mgr --> this is BLAKE
10 ;
Enter value for supplied_empno: 7844
EMPNO ENAME
---------- ----------
7499 ALLEN
7521 WARD
7654 MARTIN
7698 BLAKE
7844 TURNER
7900 JAMES
6 rows selected.
SQL>