sql03- join query

According connection table to divide, including:
       en:
             equijoins
             non-equivalent connections
           from the connection
       outer join:
             Left outer join (left join)
            right outer join (right connection) |
       fully connected (this does not write, very few use!)

 

 

Case: Find out the name of every employee of the department, required to display employee name and department name. 

         select e.ename,d.dname from emp e,dept d where e.deptno=d.deptno;  

          

 

 

 

         Cartesian product phenomenon: when two table join query, without any conditions limiting the final number of query result is the product of two tables of the number of records.

         About alias tables:
               the SELECT e.ename, d.dname from emp E, the dept d;
           alias tables What are the benefits?
                First: implementation of high efficiency.
                Second: readable.

 

        

      The equivalent of the connector connection: most important feature: the condition is equivalent relationship.

          

            SQL92: (too old, without a)
               SELECT
                     E ename, DNAME D..
                From
                      EMP E, Dept D
                 WHERE
                       E = D DEPTNO DEPTNO;..

 

              SQL99: // innex can be omitted, with the inner purpose is a good readability - some.

                 (Common)
                SELECT
                      E ename, DNAME D..
                From
                      EMP E
                Inner the Join
                      Dept D
                 ON
                      e.deptno DEPTNO = D;.
        Syntax:
                   A
                the Join
                   B
                ON
                    connection conditions
                 where

                        ...

   

     SQL99 syntax structure more clearly some of them: the connection condition table and the subsequent condition where separated

     【example】:

           select e.ename,d.dname from emp e join dept d on e.deptno=d.deptno;

            

 

  Nonequijoins connection of the connector: characterized by the largest: the relationship is non-connected conditions equivalent relationship. .
    Case: identify each employee's salary level, employees required to display the name, salary, wage scale.

     select s.grade,e.ename,e.sal from emp e join salgrade s on e.sal between s.losal and s.hisal;

     

 

   Since the connection: The greatest feature is: a table seen as two tables. Connect your own.
    Case: identify each employee's superiors, employees required to display the corresponding name and the name of leadership.

    The following table:

     

 

    select e.ename,e.mgr,p.ename,p.empno from emp e join emp p on e.mgr=p.empno;

     Outer join?

        

 

    What is the outer join, and the connector What is the difference? 

 

        En:
         
   Suppose tables A and B are connected, the connection used, all tables A and B can be matched to the table on the recording _ check out, that is the connection.
             
AB two tables is not the main points of the deputy, two tables are equal.
       Outer connecting:
           
 Let A and B tables are connected, using external connections, then, AB two tables with a table is a primary table, a table is a sub-table, the main query data in the main table, incidentally query sub-table, when the secondary It does not match the data in the table and the data in the main table. on the side tables automatically matching simulated NULL.

     ? Classification outer join
            left outer join (left join): Indicates the left side of this table is the main table.
            Right outer connector (Right link): this table indicates the right is the main table.

     External connection:
           SELECT 
               a.ename 'employee', b.ename 'leaders'
           from
               EMP A
            lelft the Join
                EMP B
           ON
               a.mgr EMPNO = B;.

           select e.ename,e.mgr,p.ename,p.empno from emp e left join emp p on e.mgr=p.empno;

           

    How three tables join query?

   Grammar:
             A
        the Join
            B

        on

              ....
        join

            C
        on

              .....
    represents: A table and the first table join Table B, A, and c in Table continued connection after the connection table. .

       Case: identify each employee's department name and salary grades

       select

              e.ename,d.dname,s.grade

        from

               emp e

         join

             dept d

         on

            e.deptno=d.deptno

         join

             salgrade s

         on e.sal between s.losal and s.hisal;

 

       

Case: Find out the name of each employee's department, wage levels, and higher collar
        

        select

            d.dname,s.grade,e1.mgr

        from

            emp e
        join

           dept d
        on

           e.deptno=d.deptno
       join

           salgrade s
        on

          e.sal between s.losal and s.hisal
        left join

            emp e1
        on

            e.mgr = e1.empno;

       

 

Guess you like

Origin www.cnblogs.com/wwww2/p/12493938.html