zoukankan      html  css  js  c++  java
  • 【练习】多表查询

    1.创建自然连接,NATURAL JOIN子句,会以两个表中具有相同名字的列为条件创建等值连接。

    SQL> select department_id,department_name,location_id,city
      2  from departments natural join locations;
    
    DEPARTMENT_ID DEPARTMENT_NAME                LOCATION_ID
    ------------- ------------------------------ -----------
    CITY
    ------------------------------
               60 IT                                    1400
    Southlake
    
               50 Shipping                              1500
    South San Francisco
    
               10 Administration                        1700
    Seattle
    
    
    DEPARTMENT_ID DEPARTMENT_NAME                LOCATION_ID
    ------------- ------------------------------ -----------
    CITY
    ------------------------------
               30 Purchasing                            1700
    Seattle
    
               90 Executive                             1700
    Seattle
    
              100 Finance                               1700
    Seattle
    
    
    DEPARTMENT_ID DEPARTMENT_NAME                LOCATION_ID
    ------------- ------------------------------ -----------
    CITY
    ------------------------------
              110 Accounting                            1700
    Seattle
    
              120 Treasury                              1700
    Seattle
    
              130 Corporate Tax                         1700
    Seattle
    
    
    DEPARTMENT_ID DEPARTMENT_NAME                LOCATION_ID
    ------------- ------------------------------ -----------
    CITY
    ------------------------------
              140 Control And Credit                    1700
    Seattle
    
              150 Shareholder Services                  1700
    Seattle
    
              160 Benefits                              1700
    Seattle
    
    
    DEPARTMENT_ID DEPARTMENT_NAME                LOCATION_ID
    ------------- ------------------------------ -----------
    CITY
    ------------------------------
              170 Manufacturing                         1700
    Seattle
    
              180 Construction                          1700
    Seattle
    
              190 Contracting                           1700
    Seattle
    
    
    DEPARTMENT_ID DEPARTMENT_NAME                LOCATION_ID
    ------------- ------------------------------ -----------
    CITY
    ------------------------------
              200 Operations                            1700
    Seattle
    
              210 IT Support                            1700
    Seattle
    
              220 NOC                                   1700
    Seattle
    
    
    DEPARTMENT_ID DEPARTMENT_NAME                LOCATION_ID
    ------------- ------------------------------ -----------
    CITY
    ------------------------------
              230 IT Helpdesk                           1700
    Seattle
    
              240 Government Sales                      1700
    Seattle
    
              250 Retail Sales                          1700
    Seattle
    
    
    DEPARTMENT_ID DEPARTMENT_NAME                LOCATION_ID
    ------------- ------------------------------ -----------
    CITY
    ------------------------------
              260 Recruiting                            1700
    Seattle
    
              270 Payroll                               1700
    Seattle
    
               20 Marketing                             1800
    Toronto
    
    
    DEPARTMENT_ID DEPARTMENT_NAME                LOCATION_ID
    ------------- ------------------------------ -----------
    CITY
    ------------------------------
               40 Human Resources                       2400
    London
    
               80 Sales                                 2500
    Oxford
    
               70 Public Relations                      2700
    Munich
    
    
    27 rows selected.

    2.使用 USING 子句创建连接,如果多个列具有相同的名称,但自然连接的数据类型又不匹配,则可以使用using子句来指定,使用一个等值的列。

    SQL> select employee_id,last_name,location_id,department_id
      2  from employees join departments using (department_id);
    
    EMPLOYEE_ID LAST_NAME                 LOCATION_ID DEPARTMENT_ID
    ----------- ------------------------- ----------- -------------
            200 Whalen                           1700            10
            201 Hartstein                        1800            20
            202 Fay                              1800            20
            114 Raphaely                         1700            30
            119 Colmenares                       1700            30
            115 Khoo                             1700            30
            116 Baida                            1700            30
            117 Tobias                           1700            30
            118 Himuro                           1700            30
            203 Mavris                           2400            40
            198 OConnell                         1500            50
    
    EMPLOYEE_ID LAST_NAME                 LOCATION_ID DEPARTMENT_ID
    ----------- ------------------------- ----------- -------------
            199 Grant                            1500            50
            120 Weiss                            1500            50
            121 Fripp                            1500            50
            122 Kaufling                         1500            50
            123 Vollman                          1500            50
            124 Mourgos                          1500            50
            125 Nayer                            1500            50
            126 Mikkilineni                      1500            50
            127 Landry                           1500            50
            128 Markle                           1500            50
            129 Bissot                           1500            50
    
    EMPLOYEE_ID LAST_NAME                 LOCATION_ID DEPARTMENT_ID
    ----------- ------------------------- ----------- -------------
            130 Atkinson                         1500            50
            131 Marlow                           1500            50
            132 Olson                            1500            50
            133 Mallin                           1500            50
            134 Rogers                           1500            50
            135 Gee                              1500            50
            136 Philtanker                       1500            50
            137 Ladwig                           1500            50
            138 Stiles                           1500            50
            139 Seo                              1500            50
            140 Patel                            1500            50
    
    EMPLOYEE_ID LAST_NAME                 LOCATION_ID DEPARTMENT_ID
    ----------- ------------------------- ----------- -------------
            141 Rajs                             1500            50
            142 Davies                           1500            50
            143 Matos                            1500            50
            144 Vargas                           1500            50
            180 Taylor                           1500            50
            181 Fleaur                           1500            50
            182 Sullivan                         1500            50
            183 Geoni                            1500            50
            184 Sarchand                         1500            50
            185 Bull                             1500            50
            186 Dellinger                        1500            50
    
    EMPLOYEE_ID LAST_NAME                 LOCATION_ID DEPARTMENT_ID
    ----------- ------------------------- ----------- -------------
            187 Cabrio                           1500            50
            188 Chung                            1500            50
            189 Dilly                            1500            50
            190 Gates                            1500            50
            191 Perkins                          1500            50
            192 Bell                             1500            50
            193 Everett                          1500            50
            194 McCain                           1500            50
            195 Jones                            1500            50
            196 Walsh                            1500            50
            197 Feeney                           1500            50
    
    EMPLOYEE_ID LAST_NAME                 LOCATION_ID DEPARTMENT_ID
    ----------- ------------------------- ----------- -------------
            104 Ernst                            1400            60
            103 Hunold                           1400            60
            107 Lorentz                          1400            60
            106 Pataballa                        1400            60
            105 Austin                           1400            60
            204 Baer                             2700            70
            176 Taylor                           2500            80
            177 Livingston                       2500            80
            179 Johnson                          2500            80
            175 Hutton                           2500            80
            174 Abel                             2500            80
    
    EMPLOYEE_ID LAST_NAME                 LOCATION_ID DEPARTMENT_ID
    ----------- ------------------------- ----------- -------------
            173 Kumar                            2500            80
            172 Bates                            2500            80
            171 Smith                            2500            80
            170 Fox                              2500            80
            169 Bloom                            2500            80
            168 Ozer                             2500            80
            145 Russell                          2500            80
            146 Partners                         2500            80
            147 Errazuriz                        2500            80
            148 Cambrault                        2500            80
            149 Zlotkey                          2500            80
    
    EMPLOYEE_ID LAST_NAME                 LOCATION_ID DEPARTMENT_ID
    ----------- ------------------------- ----------- -------------
            150 Tucker                           2500            80
            151 Bernstein                        2500            80
            152 Hall                             2500            80
            153 Olsen                            2500            80
            154 Cambrault                        2500            80
            155 Tuvault                          2500            80
            156 King                             2500            80
            157 Sully                            2500            80
            158 McEwen                           2500            80
            159 Smith                            2500            80
            160 Doran                            2500            80
    
    EMPLOYEE_ID LAST_NAME                 LOCATION_ID DEPARTMENT_ID
    ----------- ------------------------- ----------- -------------
            161 Sewall                           2500            80
            162 Vishney                          2500            80
            163 Greene                           2500            80
            164 Marvins                          2500            80
            165 Lee                              2500            80
            166 Ande                             2500            80
            167 Banda                            2500            80
            101 Kochhar                          1700            90
            100 King                             1700            90
            102 De Haan                          1700            90
            110 Chen                             1700           100
    
    EMPLOYEE_ID LAST_NAME                 LOCATION_ID DEPARTMENT_ID
    ----------- ------------------------- ----------- -------------
            108 Greenberg                        1700           100
            111 Sciarra                          1700           100
            112 Urman                            1700           100
            113 Popp                             1700           100
            109 Faviet                           1700           100
            206 Gietz                            1700           110
            205 Higgins                          1700           110
    
    106 rows selected.

    3.自然连接中是以具有相同名字的列为连接条件的,使用ON子句指定要连接任意条件或指定列连接条件。

    SQL> select e.employee_id,e.last_name,e.department_id,d.department_id,d.location_id
      2  from employees e join departments d on (e.department_id = d.department_id);
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_ID LOCATION_ID
    ----------- ------------------------- ------------- ------------- -----------
            200 Whalen                               10            10        1700
            201 Hartstein                            20            20        1800
            202 Fay                                  20            20        1800
            114 Raphaely                             30            30        1700
            119 Colmenares                           30            30        1700
            115 Khoo                                 30            30        1700
            116 Baida                                30            30        1700
            117 Tobias                               30            30        1700
            118 Himuro                               30            30        1700
            203 Mavris                               40            40        2400
            198 OConnell                             50            50        1500
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_ID LOCATION_ID
    ----------- ------------------------- ------------- ------------- -----------
            199 Grant                                50            50        1500
            120 Weiss                                50            50        1500
            121 Fripp                                50            50        1500
            122 Kaufling                             50            50        1500
            123 Vollman                              50            50        1500
            124 Mourgos                              50            50        1500
            125 Nayer                                50            50        1500
            126 Mikkilineni                          50            50        1500
            127 Landry                               50            50        1500
            128 Markle                               50            50        1500
            129 Bissot                               50            50        1500
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_ID LOCATION_ID
    ----------- ------------------------- ------------- ------------- -----------
            130 Atkinson                             50            50        1500
            131 Marlow                               50            50        1500
            132 Olson                                50            50        1500
            133 Mallin                               50            50        1500
            134 Rogers                               50            50        1500
            135 Gee                                  50            50        1500
            136 Philtanker                           50            50        1500
            137 Ladwig                               50            50        1500
            138 Stiles                               50            50        1500
            139 Seo                                  50            50        1500
            140 Patel                                50            50        1500
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_ID LOCATION_ID
    ----------- ------------------------- ------------- ------------- -----------
            141 Rajs                                 50            50        1500
            142 Davies                               50            50        1500
            143 Matos                                50            50        1500
            144 Vargas                               50            50        1500
            180 Taylor                               50            50        1500
            181 Fleaur                               50            50        1500
            182 Sullivan                             50            50        1500
            183 Geoni                                50            50        1500
            184 Sarchand                             50            50        1500
            185 Bull                                 50            50        1500
            186 Dellinger                            50            50        1500
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_ID LOCATION_ID
    ----------- ------------------------- ------------- ------------- -----------
            187 Cabrio                               50            50        1500
            188 Chung                                50            50        1500
            189 Dilly                                50            50        1500
            190 Gates                                50            50        1500
            191 Perkins                              50            50        1500
            192 Bell                                 50            50        1500
            193 Everett                              50            50        1500
            194 McCain                               50            50        1500
            195 Jones                                50            50        1500
            196 Walsh                                50            50        1500
            197 Feeney                               50            50        1500
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_ID LOCATION_ID
    ----------- ------------------------- ------------- ------------- -----------
            104 Ernst                                60            60        1400
            103 Hunold                               60            60        1400
            107 Lorentz                              60            60        1400
            106 Pataballa                            60            60        1400
            105 Austin                               60            60        1400
            204 Baer                                 70            70        2700
            176 Taylor                               80            80        2500
            177 Livingston                           80            80        2500
            179 Johnson                              80            80        2500
            175 Hutton                               80            80        2500
            174 Abel                                 80            80        2500
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_ID LOCATION_ID
    ----------- ------------------------- ------------- ------------- -----------
            173 Kumar                                80            80        2500
            172 Bates                                80            80        2500
            171 Smith                                80            80        2500
            170 Fox                                  80            80        2500
            169 Bloom                                80            80        2500
            168 Ozer                                 80            80        2500
            145 Russell                              80            80        2500
            146 Partners                             80            80        2500
            147 Errazuriz                            80            80        2500
            148 Cambrault                            80            80        2500
            149 Zlotkey                              80            80        2500
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_ID LOCATION_ID
    ----------- ------------------------- ------------- ------------- -----------
            150 Tucker                               80            80        2500
            151 Bernstein                            80            80        2500
            152 Hall                                 80            80        2500
            153 Olsen                                80            80        2500
            154 Cambrault                            80            80        2500
            155 Tuvault                              80            80        2500
            156 King                                 80            80        2500
            157 Sully                                80            80        2500
            158 McEwen                               80            80        2500
            159 Smith                                80            80        2500
            160 Doran                                80            80        2500
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_ID LOCATION_ID
    ----------- ------------------------- ------------- ------------- -----------
            161 Sewall                               80            80        2500
            162 Vishney                              80            80        2500
            163 Greene                               80            80        2500
            164 Marvins                              80            80        2500
            165 Lee                                  80            80        2500
            166 Ande                                 80            80        2500
            167 Banda                                80            80        2500
            101 Kochhar                              90            90        1700
            100 King                                 90            90        1700
            102 De Haan                              90            90        1700
            110 Chen                                100           100        1700
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_ID LOCATION_ID
    ----------- ------------------------- ------------- ------------- -----------
            108 Greenberg                           100           100        1700
            111 Sciarra                             100           100        1700
            112 Urman                               100           100        1700
            113 Popp                                100           100        1700
            109 Faviet                              100           100        1700
            206 Gietz                               110           110        1700
            205 Higgins                             110           110        1700
    
    106 rows selected.

    4.使用ON 子句创建多表连接

    SQL> select employee_id,city,department_name
      2  from employees e join departments d on d.department_id = e.department_id
      3  join locations l on d.location_id = l.location_id;
    
    EMPLOYEE_ID CITY                           DEPARTMENT_NAME
    ----------- ------------------------------ ------------------------------
            100 Seattle                        Executive
            101 Seattle                        Executive
            102 Seattle                        Executive
            103 Southlake                      IT
            104 Southlake                      IT
            105 Southlake                      IT
            106 Southlake                      IT
            107 Southlake                      IT
            108 Seattle                        Finance
            109 Seattle                        Finance
            110 Seattle                        Finance
    
    EMPLOYEE_ID CITY                           DEPARTMENT_NAME
    ----------- ------------------------------ ------------------------------
            111 Seattle                        Finance
            112 Seattle                        Finance
            113 Seattle                        Finance
            114 Seattle                        Purchasing
            115 Seattle                        Purchasing
            116 Seattle                        Purchasing
            117 Seattle                        Purchasing
            118 Seattle                        Purchasing
            119 Seattle                        Purchasing
            120 South San Francisco            Shipping
            121 South San Francisco            Shipping
    
    EMPLOYEE_ID CITY                           DEPARTMENT_NAME
    ----------- ------------------------------ ------------------------------
            122 South San Francisco            Shipping
            123 South San Francisco            Shipping
            124 South San Francisco            Shipping
            125 South San Francisco            Shipping
            126 South San Francisco            Shipping
            127 South San Francisco            Shipping
            128 South San Francisco            Shipping
            129 South San Francisco            Shipping
            130 South San Francisco            Shipping
            131 South San Francisco            Shipping
            132 South San Francisco            Shipping
    
    EMPLOYEE_ID CITY                           DEPARTMENT_NAME
    ----------- ------------------------------ ------------------------------
            133 South San Francisco            Shipping
            134 South San Francisco            Shipping
            135 South San Francisco            Shipping
            136 South San Francisco            Shipping
            137 South San Francisco            Shipping
            138 South San Francisco            Shipping
            139 South San Francisco            Shipping
            140 South San Francisco            Shipping
            141 South San Francisco            Shipping
            142 South San Francisco            Shipping
            143 South San Francisco            Shipping
    
    EMPLOYEE_ID CITY                           DEPARTMENT_NAME
    ----------- ------------------------------ ------------------------------
            144 South San Francisco            Shipping
            145 Oxford                         Sales
            146 Oxford                         Sales
            147 Oxford                         Sales
            148 Oxford                         Sales
            149 Oxford                         Sales
            150 Oxford                         Sales
            151 Oxford                         Sales
            152 Oxford                         Sales
            153 Oxford                         Sales
            154 Oxford                         Sales
    
    EMPLOYEE_ID CITY                           DEPARTMENT_NAME
    ----------- ------------------------------ ------------------------------
            155 Oxford                         Sales
            156 Oxford                         Sales
            157 Oxford                         Sales
            158 Oxford                         Sales
            159 Oxford                         Sales
            160 Oxford                         Sales
            161 Oxford                         Sales
            162 Oxford                         Sales
            163 Oxford                         Sales
            164 Oxford                         Sales
            165 Oxford                         Sales
    
    EMPLOYEE_ID CITY                           DEPARTMENT_NAME
    ----------- ------------------------------ ------------------------------
            166 Oxford                         Sales
            167 Oxford                         Sales
            168 Oxford                         Sales
            169 Oxford                         Sales
            170 Oxford                         Sales
            171 Oxford                         Sales
            172 Oxford                         Sales
            173 Oxford                         Sales
            174 Oxford                         Sales
            175 Oxford                         Sales
            176 Oxford                         Sales
    
    EMPLOYEE_ID CITY                           DEPARTMENT_NAME
    ----------- ------------------------------ ------------------------------
            177 Oxford                         Sales
            179 Oxford                         Sales
            180 South San Francisco            Shipping
            181 South San Francisco            Shipping
            182 South San Francisco            Shipping
            183 South San Francisco            Shipping
            184 South San Francisco            Shipping
            185 South San Francisco            Shipping
            186 South San Francisco            Shipping
            187 South San Francisco            Shipping
            188 South San Francisco            Shipping
    
    EMPLOYEE_ID CITY                           DEPARTMENT_NAME
    ----------- ------------------------------ ------------------------------
            189 South San Francisco            Shipping
            190 South San Francisco            Shipping
            191 South San Francisco            Shipping
            192 South San Francisco            Shipping
            193 South San Francisco            Shipping
            194 South San Francisco            Shipping
            195 South San Francisco            Shipping
            196 South San Francisco            Shipping
            197 South San Francisco            Shipping
            198 South San Francisco            Shipping
            199 South San Francisco            Shipping
    
    EMPLOYEE_ID CITY                           DEPARTMENT_NAME
    ----------- ------------------------------ ------------------------------
            200 Seattle                        Administration
            201 Toronto                        Marketing
            202 Toronto                        Marketing
            203 London                         Human Resources
            204 Munich                         Public Relations
            205 Seattle                        Accounting
            206 Seattle                        Accounting
    
    106 rows selected.

    5.使用AND子句或WHERE子句适用附加条件:

    ①SQL> SELECT e.employee_id, e.last_name, e.department_id, 
           d.department_id, d.l  2  ocation_id
    FROM   employees e JOIN departments d
    ON     (e.department_id = d.department_id)
    AND    e.manager_id = 149 ;
      3    4    5  
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_ID LOCATION_ID
    ----------- ------------------------- ------------- ------------- -----------
            174 Abel                                 80            80        2500
            175 Hutton                               80            80        2500
            179 Johnson                              80            80        2500
            177 Livingston                           80            80        2500
            176 Taylor                               80            80        2500
    ②SQL> SELECT e.employee_id, e.last_name, e.department_id, 
           d.department_id, d.location_id
    FROM   employees e JOIN departments d
    ON     (e.department_id = d.dep  2    3    4  artment_id)
    WHERE   e.manager_id = 149 ;
      5  
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_ID LOCATION_ID
    ----------- ------------------------- ------------- ------------- -----------
            174 Abel                                 80            80        2500
            175 Hutton                               80            80        2500
            179 Johnson                              80            80        2500
            177 Livingston                           80            80        2500
            176 Taylor                               80            80        2500

    6.左右连接:

    ①SQL> SELECT e.last_name, e.department_id, d.department_name
    FROM   employees e LEFT O  2  UTER JOIN departments d
    ON   (e.department_id = d.department_id) ;
      3  
    LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ------------------------- ------------- ------------------------------
    Whalen                               10 Administration
    Fay                                  20 Marketing
    Hartstein                            20 Marketing
    Colmenares                           30 Purchasing
    Himuro                               30 Purchasing
    Tobias                               30 Purchasing
    Baida                                30 Purchasing
    Khoo                                 30 Purchasing
    Raphaely                             30 Purchasing
    Mavris                               40 Human Resources
    Feeney                               50 Shipping
    
    LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ------------------------- ------------- ------------------------------
    Walsh                                50 Shipping
    Jones                                50 Shipping
    McCain                               50 Shipping
    Everett                              50 Shipping
    Bell                                 50 Shipping
    Perkins                              50 Shipping
    Gates                                50 Shipping
    Dilly                                50 Shipping
    Chung                                50 Shipping
    Cabrio                               50 Shipping
    Dellinger                            50 Shipping
    
    LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ------------------------- ------------- ------------------------------
    Bull                                 50 Shipping
    Sarchand                             50 Shipping
    Geoni                                50 Shipping
    Sullivan                             50 Shipping
    Fleaur                               50 Shipping
    Taylor                               50 Shipping
    Vargas                               50 Shipping
    Matos                                50 Shipping
    Davies                               50 Shipping
    Rajs                                 50 Shipping
    Patel                                50 Shipping
    
    LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ------------------------- ------------- ------------------------------
    Seo                                  50 Shipping
    Stiles                               50 Shipping
    Ladwig                               50 Shipping
    Philtanker                           50 Shipping
    Gee                                  50 Shipping
    Rogers                               50 Shipping
    Mallin                               50 Shipping
    Olson                                50 Shipping
    Marlow                               50 Shipping
    Atkinson                             50 Shipping
    Bissot                               50 Shipping
    
    LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ------------------------- ------------- ------------------------------
    Markle                               50 Shipping
    Landry                               50 Shipping
    Mikkilineni                          50 Shipping
    Nayer                                50 Shipping
    Mourgos                              50 Shipping
    Vollman                              50 Shipping
    Kaufling                             50 Shipping
    Fripp                                50 Shipping
    Weiss                                50 Shipping
    Grant                                50 Shipping
    OConnell                             50 Shipping
    
    LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ------------------------- ------------- ------------------------------
    Lorentz                              60 IT
    Pataballa                            60 IT
    Austin                               60 IT
    Ernst                                60 IT
    Hunold                               60 IT
    Baer                                 70 Public Relations
    Johnson                              80 Sales
    Livingston                           80 Sales
    Taylor                               80 Sales
    Hutton                               80 Sales
    Abel                                 80 Sales
    
    LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ------------------------- ------------- ------------------------------
    Kumar                                80 Sales
    Bates                                80 Sales
    Smith                                80 Sales
    Fox                                  80 Sales
    Bloom                                80 Sales
    Ozer                                 80 Sales
    Banda                                80 Sales
    Ande                                 80 Sales
    Lee                                  80 Sales
    Marvins                              80 Sales
    Greene                               80 Sales
    
    LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ------------------------- ------------- ------------------------------
    Vishney                              80 Sales
    Sewall                               80 Sales
    Doran                                80 Sales
    Smith                                80 Sales
    McEwen                               80 Sales
    Sully                                80 Sales
    King                                 80 Sales
    Tuvault                              80 Sales
    Cambrault                            80 Sales
    Olsen                                80 Sales
    Hall                                 80 Sales
    
    LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ------------------------- ------------- ------------------------------
    Bernstein                            80 Sales
    Tucker                               80 Sales
    Zlotkey                              80 Sales
    Cambrault                            80 Sales
    Errazuriz                            80 Sales
    Partners                             80 Sales
    Russell                              80 Sales
    De Haan                              90 Executive
    Kochhar                              90 Executive
    King                                 90 Executive
    Popp                                100 Finance
    
    LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ------------------------- ------------- ------------------------------
    Urman                               100 Finance
    Sciarra                             100 Finance
    Chen                                100 Finance
    Faviet                              100 Finance
    Greenberg                           100 Finance
    Gietz                               110 Accounting
    Higgins                             110 Accounting
    Grant
    
    107 rows selected.
    
    ②SQL> SELECT e.last_name, e.department_id, d.department_name
    FROM   employees e RIGHT OUTER JOIN departments d
    ON    (e.department_id = d.department_id) ;
      2    3  
    LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ------------------------- ------------- ------------------------------
    Whalen                               10 Administration
    Fay                                  20 Marketing
    Hartstein                            20 Marketing
    Tobias                               30 Purchasing
    Colmenares                           30 Purchasing
    Baida                                30 Purchasing
    Raphaely                             30 Purchasing
    Khoo                                 30 Purchasing
    Himuro                               30 Purchasing
    Mavris                               40 Human Resources
    Feeney                               50 Shipping
    
    LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ------------------------- ------------- ------------------------------
    Fleaur                               50 Shipping
    Fripp                                50 Shipping
    Gates                                50 Shipping
    Gee                                  50 Shipping
    Geoni                                50 Shipping
    Grant                                50 Shipping
    Jones                                50 Shipping
    Kaufling                             50 Shipping
    Ladwig                               50 Shipping
    Everett                              50 Shipping
    Dilly                                50 Shipping
    
    LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ------------------------- ------------- ------------------------------
    Dellinger                            50 Shipping
    Davies                               50 Shipping
    Chung                                50 Shipping
    Cabrio                               50 Shipping
    Bull                                 50 Shipping
    Bissot                               50 Shipping
    Bell                                 50 Shipping
    Atkinson                             50 Shipping
    Landry                               50 Shipping
    Weiss                                50 Shipping
    Walsh                                50 Shipping
    
    LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ------------------------- ------------- ------------------------------
    Vollman                              50 Shipping
    Vargas                               50 Shipping
    Taylor                               50 Shipping
    Mallin                               50 Shipping
    Markle                               50 Shipping
    Marlow                               50 Shipping
    Matos                                50 Shipping
    McCain                               50 Shipping
    Mikkilineni                          50 Shipping
    Mourgos                              50 Shipping
    Nayer                                50 Shipping
    
    LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ------------------------- ------------- ------------------------------
    OConnell                             50 Shipping
    Olson                                50 Shipping
    Patel                                50 Shipping
    Perkins                              50 Shipping
    Philtanker                           50 Shipping
    Rajs                                 50 Shipping
    Rogers                               50 Shipping
    Sarchand                             50 Shipping
    Seo                                  50 Shipping
    Stiles                               50 Shipping
    Sullivan                             50 Shipping
    
    LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ------------------------- ------------- ------------------------------
    Austin                               60 IT
    Pataballa                            60 IT
    Ernst                                60 IT
    Lorentz                              60 IT
    Hunold                               60 IT
    Baer                                 70 Public Relations
    Abel                                 80 Sales
    Ande                                 80 Sales
    Banda                                80 Sales
    Bates                                80 Sales
    Bernstein                            80 Sales
    
    LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ------------------------- ------------- ------------------------------
    Bloom                                80 Sales
    Cambrault                            80 Sales
    Cambrault                            80 Sales
    Doran                                80 Sales
    Errazuriz                            80 Sales
    Fox                                  80 Sales
    Greene                               80 Sales
    Hall                                 80 Sales
    Hutton                               80 Sales
    Johnson                              80 Sales
    King                                 80 Sales
    
    LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ------------------------- ------------- ------------------------------
    Kumar                                80 Sales
    Lee                                  80 Sales
    Livingston                           80 Sales
    Marvins                              80 Sales
    McEwen                               80 Sales
    Olsen                                80 Sales
    Ozer                                 80 Sales
    Partners                             80 Sales
    Russell                              80 Sales
    Sewall                               80 Sales
    Smith                                80 Sales
    
    LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ------------------------- ------------- ------------------------------
    Smith                                80 Sales
    Sully                                80 Sales
    Taylor                               80 Sales
    Tucker                               80 Sales
    Tuvault                              80 Sales
    Vishney                              80 Sales
    Zlotkey                              80 Sales
    Kochhar                              90 Executive
    King                                 90 Executive
    De Haan                              90 Executive
    Popp                                100 Finance
    
    LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ------------------------- ------------- ------------------------------
    Urman                               100 Finance
    Chen                                100 Finance
    Faviet                              100 Finance
    Sciarra                             100 Finance
    Greenberg                           100 Finance
    Gietz                               110 Accounting
    Higgins                             110 Accounting
                                            Treasury
                                            Corporate Tax
                                            Control And Credit
                                            Shareholder Services
    
    LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ------------------------- ------------- ------------------------------
                                            Benefits
                                            Manufacturing
                                            Construction
                                            Contracting
                                            Operations
                                            IT Support
                                            NOC
                                            IT Helpdesk
                                            Government Sales
                                            Retail Sales
                                            Recruiting
    
    LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ------------------------- ------------- ------------------------------
                                            Payroll
    
    122 rows selected.
  • 相关阅读:
    在Visual Studio中怎样快速添加代码段
    18个不常见的C#关键字,您使用过几个?
    C# 非常好用的组元Tuple
    C# List根据另一个List集合或数组排序
    Expression 核心操作符、表达式、操作方法
    如何避免频繁创建临时对象
    C# 23种设计模式
    C# 23种设计模式
    Api Cloud官方日期类型转换
    sql server 保留小数(续A)
  • 原文地址:https://www.cnblogs.com/tomatoes-/p/6071357.html
Copyright © 2011-2022 走看看