Monday, 13 February 2012

SELF JOIN

    =>    A self join is performed on the tables having self referential integrity.
    =>    To perform self join, same table  must be listed twice with different alias.

    Syntax:
    =>    Select <collist>
        From <tablename>  as t1, <tablename> as t2
        where <join condition>
   
    Examples:
   
    1. Display employee name and his respective manager name ?
    =>    Select e.ename, m.ename from emp e, emp m where e.mgr = m.empno;
            (or)
        Select e.ename, m.ename from emp e JOIN emp m ON e.mgr = m.empno;

    2. Display the names of the employee who earns more than his manager ?
    =>    Select e.ename, m.ename from emp e, emp m where e.mgr = m.empno and e.sal > m.sal;
            (or)
        Select e.ename, m.ename from emp e JOIN emp m ON e.mgr = m.empno and e.sal > m.sal;

    3. Display the Manager of Blake ?
    =>    Select e.ename from emp e, emp m where e.mgr = m.empno and m.ename = 'BLAKE';
            (or)
        Select e.ename from emp e JOIN emp m ON e.mgr = m.empno and m.ename = 'BLAKE';


No comments:

Post a Comment