Q: List the Emps who are senior to their own MGRS.
select * from emp e,emp m where e.mgr = m.empno and e.hiredate < m.hiredate;
select * from emp e,emp m where e.empno= m.mgr and e.hiredate > m.hiredate;
the above two queries are getting same results but i want to know the exact difference between those two queries.
It becomes clearer when we take out hiredate, and add some aliases to the columns
SQL> select e.*, m.*
2 from emp e,emp m
3 where e.mgr = m.empno;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7902 FORD ANALYST 7566 03-DEC-81 3000 20 7566 JONES MANAGER 7839 02-APR-81 1232 20
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20 7566 JONES MANAGER 7839 02-APR-81 1232 20
7844 TURNER SALESMAN 7698 08-SEP-81 1500 30 7698 BLAKE MANAGER 7839 01-MAY-81 1232 30
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7698 BLAKE MANAGER 7839 01-MAY-81 1232 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 7698 BLAKE MANAGER 7839 01-MAY-81 1232 30
7900 JAMES CLERK 7698 03-DEC-81 950 30 7698 BLAKE MANAGER 7839 01-MAY-81 1232 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7698 BLAKE MANAGER 7839 01-MAY-81 1232 30
7934 MILLER CLERK 7782 23-JAN-82 1300 10 7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7876 ADAMS CLERK 7788 12-JAN-83 1100 20 7788 SCOTT ANALYST 7566 09-DEC-82 3000 20
7698 BLAKE MANAGER 7839 01-MAY-81 1232 30 7839 KING PRESIDENT 17-NOV-81 5000 10
7566 JONES MANAGER 7839 02-APR-81 1232 20 7839 KING PRESIDENT 17-NOV-81 5000 10
7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7839 KING PRESIDENT 17-NOV-81 5000 10
7369 SMITH CLERK 7902 17-DEC-80 800 20 7902 FORD ANALYST 7566 03-DEC-81 3000 20
13 rows selected.
SQL> select e.*, m.*
2 from emp e,emp m
3 where e.empno= m.mgr ;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7566 JONES MANAGER 7839 02-APR-81 1232 20 7902 FORD ANALYST 7566 03-DEC-81 3000 20
7566 JONES MANAGER 7839 02-APR-81 1232 20 7788 SCOTT ANALYST 7566 09-DEC-82 3000 20
7698 BLAKE MANAGER 7839 01-MAY-81 1232 30 7844 TURNER SALESMAN 7698 08-SEP-81 1500 30
7698 BLAKE MANAGER 7839 01-MAY-81 1232 30 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7698 BLAKE MANAGER 7839 01-MAY-81 1232 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7698 BLAKE MANAGER 7839 01-MAY-81 1232 30 7900 JAMES CLERK 7698 03-DEC-81 950 30
7698 BLAKE MANAGER 7839 01-MAY-81 1232 30 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7934 MILLER CLERK 7782 23-JAN-82 1300 10
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20 7876 ADAMS CLERK 7788 12-JAN-83 1100 20
7839 KING PRESIDENT 17-NOV-81 5000 10 7698 BLAKE MANAGER 7839 01-MAY-81 1232 30
7839 KING PRESIDENT 17-NOV-81 5000 10 7566 JONES MANAGER 7839 02-APR-81 1232 20
7839 KING PRESIDENT 17-NOV-81 5000 10 7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7902 FORD ANALYST 7566 03-DEC-81 3000 20 7369 SMITH CLERK 7902 17-DEC-80 800 20
So
query 1: "e" represents the employees, "m" represents the manager
query 2: "e" actually represents the managers, "m" represents the employees
Hence the required flipping of the "<" and ">" for the hiredate predicates.
In terms of comprehension, you could argue that the first query is "more correct" because there is not that ambiguity between the aliases used and the results.