• SQL堂上作业二


    where语句

    where语句可以进行内容的选择。

    where语句需要加在from的后面。

    SELECT employee_id, last_name, job_id, department_id
    FROM employees
    WHERE department_id = 90 ;
    

    输出如下:

    EMPLOYEE_ID LAST_NAME                                          JOB_ID               DEPARTMENT_ID
    ----------- -------------------------------------------------- -------------------- -------------
            100 King                                               AD_PRES                         90
            101 Kochhar                                            AD_VP                           90
            102 De Haan                                            AD_VP                           90
    

    查询日期或字符串

    在查询字符串或者日期的时候,关键字应当用单引号框选

    SELECT last_name, job_id, department_id
    FROM employees
    WHERE last_name = 'Whalen';
    

    输出如下:

    LAST_NAME                                          JOB_ID               DEPARTMENT_ID
    -------------------------------------------------- -------------------- -------------
    Whalen                                             AD_ASST                         10
    

    比较符号查找

    WHERE语句支持用数学符号进行条件限定

    SELECT last_name, salary
    FROM employees
    WHERE salary <= 3000;
    

    输出如下:

    LAST_NAME                                              SALARY
    -------------------------------------------------- ----------
    Baida                                                    2900
    Tobias                                                   2800
    Himuro                                                   2600
    Colmenares                                               2500
    Mikkilineni                                              2700
    Landry                                                   2400
    Markle                                                   2200
    Atkinson                                                 2800
    Marlow                                                   2500
    Olson                                                    2100
    Rogers                                                   2900
    Gee                                                      2400
    Philtanker                                               2200
    Seo                                                      2700
    Patel                                                    2500
    Matos                                                    2600
    Vargas                                                   2500
    Sullivan                                                 2500
    Geoni                                                    2800
    Cabrio                                                   3000
    Gates                                                    2900
    Perkins                                                  2500
    Jones                                                    2800
    Feeney                                                   3000
    OConnell                                                 2600
    Grant                                                    2600
    
    已选择26行。
    

    给定范围查找

    WHERE支持范围限定

    SELECT last_name, salary
    FROM employees
    WHERE salary BETWEEN 2500 AND 3500;
    

    输出如下

    LAST_NAME                                              SALARY
    -------------------------------------------------- ----------
    Khoo                                                     3100
    Baida                                                    2900
    Tobias                                                   2800
    Himuro                                                   2600
    Colmenares                                               2500
    Nayer                                                    3200
    Mikkilineni                                              2700
    Bissot                                                   3300
    Atkinson                                                 2800
    Marlow                                                   2500
    Mallin                                                   3300
    Rogers                                                   2900
    Stiles                                                   3200
    Seo                                                      2700
    Patel                                                    2500
    Rajs                                                     3500
    Davies                                                   3100
    Matos                                                    2600
    Vargas                                                   2500
    Taylor                                                   3200
    Fleaur                                                   3100
    Sullivan                                                 2500
    Geoni                                                    2800
    Dellinger                                                3400
    Cabrio                                                   3000
    Gates                                                    2900
    Perkins                                                  2500
    McCain                                                   3200
    Jones                                                    2800
    Walsh                                                    3100
    Feeney                                                   3000
    OConnell                                                 2600
    Grant                                                    2600
    
    已选择33行。
    

    IN成员

    我们可以用in成员对需要查询的成员进行限定

    SELECT employee_id, last_name, salary, manager_id
    FROM employees
    WHERE manager_id IN (100, 101, 201);
    

    输出如下

    EMPLOYEE_ID LAST_NAME                                              SALARY MANAGER_ID
    ----------- -------------------------------------------------- ---------- ----------
            101 Kochhar                                                 17000        100
            102 De Haan                                                 17000        100
            108 Greenberg                                               12000        101
            114 Raphaely                                                11000        100
            120 Weiss                                                    8000        100
            121 Fripp                                                    8200        100
            122 Kaufling                                                 7900        100
            123 Vollman                                                  6500        100
            124 Mourgos                                                  5800        100
            145 Russell                                                 14000        100
            146 Partners                                                13500        100
            147 Errazuriz                                               12000        100
            148 Cambrault                                               11000        100
            149 Zlotkey                                                 10500        100
            200 Whalen                                                   4400        101
            201 Hartstein                                               13000        100
            202 Fay                                                      6000        201
            203 Mavris                                                   6500        101
            204 Baer                                                    10000        101
            205 Higgins                                                 12000        101
    
    已选择20行。
    

    带%的模糊查询

    我们可以用LIKE加上通配符%进行字符串模糊查询

    其中%表示可以匹配任意多个字符

    SELECT first_name
    FROM employees
    WHERE first_name LIKE 'S%';
    

    输出如下

    FIRST_NAME
    ----------------------------------------
    Sundar
    Shelli
    Sarah
    Shelley
    Steven
    Sundita
    Steven
    Susan
    Samuel
    Sarath
    Stephen
    Sigal
    Shanta
    
    已选择13行。
    

    带_的模糊查询

    我们可以用LIKE加单字符匹配符\(\_\)进行模糊查询,它代表任意一个字符(注意空格也算一个字符,但不能没有字符)

    SELECT last_name
    FROM employees
    WHERE last_name LIKE '_o%';
    

    输出如下:

    LAST_NAME
    --------------------------------------------------
    Colmenares
    Doran
    Fox
    Johnson
    Jones
    Kochhar
    Lorentz
    Mourgos
    Popp
    Rogers
    Tobias
    Vollman
    
    已选择12行。
    

    空值查询

    我们可以用IS NULL语句来寻找空值

    SELECT last_name, manager_id
    FROM employees
    WHERE manager_id IS NULL;
    

    输出如下

    LAST_NAME                                          MANAGER_ID
    -------------------------------------------------- ----------
    King
    

    逻辑运算符

    WHERE语句中,如果要添加多个限制条件怎么办?

    我们可以用带逻辑运算符的表达式解决这个问题

    AND运算

    SELECT employee_id, last_name, job_id, salary
    FROM employees
    WHERE salary >=10000
    AND job_id LIKE '%MAN%';
    

    输出如下

    EMPLOYEE_ID LAST_NAME                                          JOB_ID                   SALARY
    ----------- -------------------------------------------------- -------------------- ----------
            114 Raphaely                                           PU_MAN                    11000
            145 Russell                                            SA_MAN                    14000
            146 Partners                                           SA_MAN                    13500
            147 Errazuriz                                          SA_MAN                    12000
            148 Cambrault                                          SA_MAN                    11000
            149 Zlotkey                                            SA_MAN                    10500
            201 Hartstein                                          MK_MAN                    13000
    
    已选择7行。
    

    OR运算

    SELECT employee_id, last_name, job_id, salary
    FROM employees
    WHERE salary >= 10000
    OR job_id LIKE '%MAN%';
    

    输出如下

    EMPLOYEE_ID LAST_NAME                                          JOB_ID                   SALARY
    ----------- -------------------------------------------------- -------------------- ----------
            100 King                                               AD_PRES                   24000
            101 Kochhar                                            AD_VP                     17000
            102 De Haan                                            AD_VP                     17000
            108 Greenberg                                          FI_MGR                    12000
            114 Raphaely                                           PU_MAN                    11000
            120 Weiss                                              ST_MAN                     8000
            121 Fripp                                              ST_MAN                     8200
            122 Kaufling                                           ST_MAN                     7900
            123 Vollman                                            ST_MAN                     6500
            124 Mourgos                                            ST_MAN                     5800
            145 Russell                                            SA_MAN                    14000
            146 Partners                                           SA_MAN                    13500
            147 Errazuriz                                          SA_MAN                    12000
            148 Cambrault                                          SA_MAN                    11000
            149 Zlotkey                                            SA_MAN                    10500
            150 Tucker                                             SA_REP                    10000
            156 King                                               SA_REP                    10000
            162 Vishney                                            SA_REP                    10500
            168 Ozer                                               SA_REP                    11500
            169 Bloom                                              SA_REP                    10000
            174 Abel                                               SA_REP                    11000
            201 Hartstein                                          MK_MAN                    13000
            204 Baer                                               PR_REP                    10000
            205 Higgins                                            AC_MGR                    12000
    
    已选择24行。
    

    NOT运算

    SELECT last_name, job_id
    FROM employees
    WHERE job_id 
    NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP');
    

    输出如下:

    LAST_NAME                                          JOB_ID
    -------------------------------------------------- --------------------
    King                                               AD_PRES
    Kochhar                                            AD_VP
    De Haan                                            AD_VP
    Greenberg                                          FI_MGR
    Faviet                                             FI_ACCOUNT
    Chen                                               FI_ACCOUNT
    Sciarra                                            FI_ACCOUNT
    Urman                                              FI_ACCOUNT
    Popp                                               FI_ACCOUNT
    Raphaely                                           PU_MAN
    Khoo                                               PU_CLERK
    Baida                                              PU_CLERK
    Tobias                                             PU_CLERK
    Himuro                                             PU_CLERK
    Colmenares                                         PU_CLERK
    Weiss                                              ST_MAN
    Fripp                                              ST_MAN
    Kaufling                                           ST_MAN
    Vollman                                            ST_MAN
    Mourgos                                            ST_MAN
    Russell                                            SA_MAN
    Partners                                           SA_MAN
    Errazuriz                                          SA_MAN
    Cambrault                                          SA_MAN
    Zlotkey                                            SA_MAN
    Taylor                                             SH_CLERK
    Fleaur                                             SH_CLERK
    Sullivan                                           SH_CLERK
    Geoni                                              SH_CLERK
    Sarchand                                           SH_CLERK
    Bull                                               SH_CLERK
    Dellinger                                          SH_CLERK
    Cabrio                                             SH_CLERK
    Chung                                              SH_CLERK
    Dilly                                              SH_CLERK
    Gates                                              SH_CLERK
    Perkins                                            SH_CLERK
    Bell                                               SH_CLERK
    Everett                                            SH_CLERK
    McCain                                             SH_CLERK
    Jones                                              SH_CLERK
    Walsh                                              SH_CLERK
    Feeney                                             SH_CLERK
    OConnell                                           SH_CLERK
    Grant                                              SH_CLERK
    Whalen                                             AD_ASST
    Hartstein                                          MK_MAN
    Fay                                                MK_REP
    Mavris                                             HR_REP
    Baer                                               PR_REP
    Higgins                                            AC_MGR
    Gietz                                              AC_ACCOUNT
    
    已选择52行。
    

    运算符优先级

    在SQL中,运算符优先级如下所示,数字越小优先级越高

    优先级 运算符
    1 算数运算符
    2 连接符
    3 比较运算符
    4 IS [NOT] NULL, LIKE, [NOT] IN
    5 [NOT] BETWEEN
    6 逻辑运算符NOT
    7 逻辑运算符AND
    8 逻辑运算符OR

    注意:我们可以添加括号以改变运算顺序

    在下方这个指令中,AND前后的指令会先进行计算,然后再将所得结果与OR合并计算

    SELECT last_name, job_id, salary
    FROM employees
    WHERE job_id = 'SA_REP'
    OR job_id = 'AD_PRES'
    AND salary > 15000;
    

    输出如下:

    LAST_NAME                                          JOB_ID                   SALARY
    -------------------------------------------------- -------------------- ----------
    King                                               AD_PRES                   24000
    Tucker                                             SA_REP                    10000
    Bernstein                                          SA_REP                     9500
    Hall                                               SA_REP                     9000
    Olsen                                              SA_REP                     8000
    Cambrault                                          SA_REP                     7500
    Tuvault                                            SA_REP                     7000
    King                                               SA_REP                    10000
    Sully                                              SA_REP                     9500
    McEwen                                             SA_REP                     9000
    Smith                                              SA_REP                     8000
    Doran                                              SA_REP                     7500
    Sewall                                             SA_REP                     7000
    Vishney                                            SA_REP                    10500
    Greene                                             SA_REP                     9500
    Marvins                                            SA_REP                     7200
    Lee                                                SA_REP                     6800
    Ande                                               SA_REP                     6400
    Banda                                              SA_REP                     6200
    Ozer                                               SA_REP                    11500
    Bloom                                              SA_REP                    10000
    Fox                                                SA_REP                     9600
    Smith                                              SA_REP                     7400
    Bates                                              SA_REP                     7300
    Kumar                                              SA_REP                     6100
    Abel                                               SA_REP                    11000
    Hutton                                             SA_REP                     8800
    Taylor                                             SA_REP                     8600
    Livingston                                         SA_REP                     8400
    Grant                                              SA_REP                     7000
    Johnson                                            SA_REP                     6200
    
    已选择31行。
    

    如下方指令所示,我们用括号使得OR先完成计算,再进行AND计算

    SELECT last_name, job_id, salary
    FROM employees
    WHERE (job_id = 'SA_REP'
    OR job_id = 'AD_PRES')
    AND salary > 15000;
    

    输出如下

    LAST_NAME                                          JOB_ID                   SALARY
    -------------------------------------------------- -------------------- ----------
    King                                               AD_PRES                   24000
    

    ORDER语句

    我们可以用ORDER语句对输出的行进行排序

    默认情况下,ORDER语句为从小到大排序

    SELECT last_name, job_id, department_id, hire_date
    FROM employees
    ORDER BY hire_date ;
    

    输出如下:

    LAST_NAME                                          JOB_ID               DEPARTMENT_ID HIRE_DATE
    -------------------------------------------------- -------------------- ------------- --------------
    King                                               AD_PRES                         90 17-6月 -87
    Whalen                                             AD_ASST                         10 17-9月 -87
    Kochhar                                            AD_VP                           90 21-9月 -89
    Hunold                                             IT_PROG                         60 03-1月 -90
    Ernst                                              IT_PROG                         60 21-5月 -91
    De Haan                                            AD_VP                           90 13-1月 -93
    Mavris                                             HR_REP                          40 07-6月 -94
    Baer                                               PR_REP                          70 07-6月 -94
    Higgins                                            AC_MGR                         110 07-6月 -94
    Gietz                                              AC_ACCOUNT                     110 07-6月 -94
    Faviet                                             FI_ACCOUNT                     100 16-8月 -94
    Greenberg                                          FI_MGR                         100 17-8月 -94
    Raphaely                                           PU_MAN                          30 07-12月-94
    Kaufling                                           ST_MAN                          50 01-5月 -95
    Khoo                                               PU_CLERK                        30 18-5月 -95
    Ladwig                                             ST_CLERK                        50 14-7月 -95
    Rajs                                               ST_CLERK                        50 17-10月-95
    Sarchand                                           SH_CLERK                        50 27-1月 -96
    King                                               SA_REP                          80 30-1月 -96
    Bell                                               SH_CLERK                        50 04-2月 -96
    Hartstein                                          MK_MAN                          20 17-2月 -96
    Sully                                              SA_REP                          80 04-3月 -96
    Abel                                               SA_REP                          80 11-5月 -96
    Mallin                                             ST_CLERK                        50 14-6月 -96
    Weiss                                              ST_MAN                          50 18-7月 -96
    McEwen                                             SA_REP                          80 01-8月 -96
    Russell                                            SA_MAN                          80 01-10月-96
    Partners                                           SA_MAN                          80 05-1月 -97
    Davies                                             ST_CLERK                        50 29-1月 -97
    Tucker                                             SA_REP                          80 30-1月 -97
    Marlow                                             ST_CLERK                        50 16-2月 -97
    Bull                                               SH_CLERK                        50 20-2月 -97
    Everett                                            SH_CLERK                        50 03-3月 -97
    Smith                                              SA_REP                          80 10-3月 -97
    Errazuriz                                          SA_MAN                          80 10-3月 -97
    Ozer                                               SA_REP                          80 11-3月 -97
    Hutton                                             SA_REP                          80 19-3月 -97
    Bernstein                                          SA_REP                          80 24-3月 -97
    Fripp                                              ST_MAN                          50 10-4月 -97
    Chung                                              SH_CLERK                        50 14-6月 -97
    Austin                                             IT_PROG                         60 25-6月 -97
    Nayer                                              ST_CLERK                        50 16-7月 -97
    Tobias                                             PU_CLERK                        30 24-7月 -97
    Dilly                                              SH_CLERK                        50 13-8月 -97
    Fay                                                MK_REP                          20 17-8月 -97
    Bissot                                             ST_CLERK                        50 20-8月 -97
    Hall                                               SA_REP                          80 20-8月 -97
    Chen                                               FI_ACCOUNT                     100 28-9月 -97
    Sciarra                                            FI_ACCOUNT                     100 30-9月 -97
    Vollman                                            ST_MAN                          50 10-10月-97
    Stiles                                             ST_CLERK                        50 26-10月-97
    Atkinson                                           ST_CLERK                        50 30-10月-97
    Vishney                                            SA_REP                          80 11-11月-97
    Doran                                              SA_REP                          80 15-12月-97
    Baida                                              PU_CLERK                        30 24-12月-97
    Fox                                                SA_REP                          80 24-1月 -98
    Taylor                                             SH_CLERK                        50 24-1月 -98
    Pataballa                                          IT_PROG                         60 05-2月 -98
    Seo                                                ST_CLERK                        50 12-2月 -98
    Fleaur                                             SH_CLERK                        50 23-2月 -98
    Urman                                              FI_ACCOUNT                     100 07-3月 -98
    Matos                                              ST_CLERK                        50 15-3月 -98
    Bloom                                              SA_REP                          80 23-3月 -98
    Taylor                                             SA_REP                          80 24-3月 -98
    Olsen                                              SA_REP                          80 30-3月 -98
    Patel                                              ST_CLERK                        50 06-4月 -98
    Livingston                                         SA_REP                          80 23-4月 -98
    Walsh                                              SH_CLERK                        50 24-4月 -98
    Feeney                                             SH_CLERK                        50 23-5月 -98
    Dellinger                                          SH_CLERK                        50 24-6月 -98
    McCain                                             SH_CLERK                        50 01-7月 -98
    Vargas                                             ST_CLERK                        50 09-7月 -98
    Gates                                              SH_CLERK                        50 11-7月 -98
    Rogers                                             ST_CLERK                        50 26-8月 -98
    Mikkilineni                                        ST_CLERK                        50 28-9月 -98
    Sewall                                             SA_REP                          80 03-11月-98
    Himuro                                             PU_CLERK                        30 15-11月-98
    Cambrault                                          SA_REP                          80 09-12月-98
    Landry                                             ST_CLERK                        50 14-1月 -99
    Cabrio                                             SH_CLERK                        50 07-2月 -99
    Lorentz                                            IT_PROG                         60 07-2月 -99
    Smith                                              SA_REP                          80 23-2月 -99
    Jones                                              SH_CLERK                        50 17-3月 -99
    Greene                                             SA_REP                          80 19-3月 -99
    Bates                                              SA_REP                          80 24-3月 -99
    Olson                                              ST_CLERK                        50 10-4月 -99
    Grant                                              SA_REP                             24-5月 -99
    OConnell                                           SH_CLERK                        50 21-6月 -99
    Sullivan                                           SH_CLERK                        50 21-6月 -99
    Colmenares                                         PU_CLERK                        30 10-8月 -99
    Cambrault                                          SA_MAN                          80 15-10月-99
    Mourgos                                            ST_MAN                          50 16-11月-99
    Tuvault                                            SA_REP                          80 23-11月-99
    Popp                                               FI_ACCOUNT                     100 07-12月-99
    Gee                                                ST_CLERK                        50 12-12月-99
    Perkins                                            SH_CLERK                        50 19-12月-99
    Johnson                                            SA_REP                          80 04-1月 -00
    Grant                                              SH_CLERK                        50 13-1月 -00
    Marvins                                            SA_REP                          80 24-1月 -00
    Zlotkey                                            SA_MAN                          80 29-1月 -00
    Geoni                                              SH_CLERK                        50 03-2月 -00
    Philtanker                                         ST_CLERK                        50 06-2月 -00
    Lee                                                SA_REP                          80 23-2月 -00
    Markle                                             ST_CLERK                        50 08-3月 -00
    Ande                                               SA_REP                          80 24-3月 -00
    Banda                                              SA_REP                          80 21-4月 -00
    Kumar                                              SA_REP                          80 21-4月 -00
    
    已选择107行。
    

    DESC倒序输出

    我们可以添加DESC让数据倒序输出

    SELECT last_name, job_id, department_id, hire_date
    FROM employees
    ORDER BY hire_date DESC ;
    

    输出如下:

    LAST_NAME                                          JOB_ID               DEPARTMENT_ID HIRE_DATE
    -------------------------------------------------- -------------------- ------------- --------------
    Kumar                                              SA_REP                          80 21-4月 -00
    Banda                                              SA_REP                          80 21-4月 -00
    Ande                                               SA_REP                          80 24-3月 -00
    Markle                                             ST_CLERK                        50 08-3月 -00
    Lee                                                SA_REP                          80 23-2月 -00
    Philtanker                                         ST_CLERK                        50 06-2月 -00
    Geoni                                              SH_CLERK                        50 03-2月 -00
    Zlotkey                                            SA_MAN                          80 29-1月 -00
    Marvins                                            SA_REP                          80 24-1月 -00
    Grant                                              SH_CLERK                        50 13-1月 -00
    Johnson                                            SA_REP                          80 04-1月 -00
    Perkins                                            SH_CLERK                        50 19-12月-99
    Gee                                                ST_CLERK                        50 12-12月-99
    Popp                                               FI_ACCOUNT                     100 07-12月-99
    Tuvault                                            SA_REP                          80 23-11月-99
    Mourgos                                            ST_MAN                          50 16-11月-99
    Cambrault                                          SA_MAN                          80 15-10月-99
    Colmenares                                         PU_CLERK                        30 10-8月 -99
    Sullivan                                           SH_CLERK                        50 21-6月 -99
    OConnell                                           SH_CLERK                        50 21-6月 -99
    Grant                                              SA_REP                             24-5月 -99
    Olson                                              ST_CLERK                        50 10-4月 -99
    Bates                                              SA_REP                          80 24-3月 -99
    Greene                                             SA_REP                          80 19-3月 -99
    Jones                                              SH_CLERK                        50 17-3月 -99
    Smith                                              SA_REP                          80 23-2月 -99
    Cabrio                                             SH_CLERK                        50 07-2月 -99
    Lorentz                                            IT_PROG                         60 07-2月 -99
    Landry                                             ST_CLERK                        50 14-1月 -99
    Cambrault                                          SA_REP                          80 09-12月-98
    Himuro                                             PU_CLERK                        30 15-11月-98
    Sewall                                             SA_REP                          80 03-11月-98
    Mikkilineni                                        ST_CLERK                        50 28-9月 -98
    Rogers                                             ST_CLERK                        50 26-8月 -98
    Gates                                              SH_CLERK                        50 11-7月 -98
    Vargas                                             ST_CLERK                        50 09-7月 -98
    McCain                                             SH_CLERK                        50 01-7月 -98
    Dellinger                                          SH_CLERK                        50 24-6月 -98
    Feeney                                             SH_CLERK                        50 23-5月 -98
    Walsh                                              SH_CLERK                        50 24-4月 -98
    Livingston                                         SA_REP                          80 23-4月 -98
    Patel                                              ST_CLERK                        50 06-4月 -98
    Olsen                                              SA_REP                          80 30-3月 -98
    Taylor                                             SA_REP                          80 24-3月 -98
    Bloom                                              SA_REP                          80 23-3月 -98
    Matos                                              ST_CLERK                        50 15-3月 -98
    Urman                                              FI_ACCOUNT                     100 07-3月 -98
    Fleaur                                             SH_CLERK                        50 23-2月 -98
    Seo                                                ST_CLERK                        50 12-2月 -98
    Pataballa                                          IT_PROG                         60 05-2月 -98
    Taylor                                             SH_CLERK                        50 24-1月 -98
    Fox                                                SA_REP                          80 24-1月 -98
    Baida                                              PU_CLERK                        30 24-12月-97
    Doran                                              SA_REP                          80 15-12月-97
    Vishney                                            SA_REP                          80 11-11月-97
    Atkinson                                           ST_CLERK                        50 30-10月-97
    Stiles                                             ST_CLERK                        50 26-10月-97
    Vollman                                            ST_MAN                          50 10-10月-97
    Sciarra                                            FI_ACCOUNT                     100 30-9月 -97
    Chen                                               FI_ACCOUNT                     100 28-9月 -97
    Hall                                               SA_REP                          80 20-8月 -97
    Bissot                                             ST_CLERK                        50 20-8月 -97
    Fay                                                MK_REP                          20 17-8月 -97
    Dilly                                              SH_CLERK                        50 13-8月 -97
    Tobias                                             PU_CLERK                        30 24-7月 -97
    Nayer                                              ST_CLERK                        50 16-7月 -97
    Austin                                             IT_PROG                         60 25-6月 -97
    Chung                                              SH_CLERK                        50 14-6月 -97
    Fripp                                              ST_MAN                          50 10-4月 -97
    Bernstein                                          SA_REP                          80 24-3月 -97
    Hutton                                             SA_REP                          80 19-3月 -97
    Ozer                                               SA_REP                          80 11-3月 -97
    Smith                                              SA_REP                          80 10-3月 -97
    Errazuriz                                          SA_MAN                          80 10-3月 -97
    Everett                                            SH_CLERK                        50 03-3月 -97
    Bull                                               SH_CLERK                        50 20-2月 -97
    Marlow                                             ST_CLERK                        50 16-2月 -97
    Tucker                                             SA_REP                          80 30-1月 -97
    Davies                                             ST_CLERK                        50 29-1月 -97
    Partners                                           SA_MAN                          80 05-1月 -97
    Russell                                            SA_MAN                          80 01-10月-96
    McEwen                                             SA_REP                          80 01-8月 -96
    Weiss                                              ST_MAN                          50 18-7月 -96
    Mallin                                             ST_CLERK                        50 14-6月 -96
    Abel                                               SA_REP                          80 11-5月 -96
    Sully                                              SA_REP                          80 04-3月 -96
    Hartstein                                          MK_MAN                          20 17-2月 -96
    Bell                                               SH_CLERK                        50 04-2月 -96
    King                                               SA_REP                          80 30-1月 -96
    Sarchand                                           SH_CLERK                        50 27-1月 -96
    Rajs                                               ST_CLERK                        50 17-10月-95
    Ladwig                                             ST_CLERK                        50 14-7月 -95
    Khoo                                               PU_CLERK                        30 18-5月 -95
    Kaufling                                           ST_MAN                          50 01-5月 -95
    Raphaely                                           PU_MAN                          30 07-12月-94
    Greenberg                                          FI_MGR                         100 17-8月 -94
    Faviet                                             FI_ACCOUNT                     100 16-8月 -94
    Mavris                                             HR_REP                          40 07-6月 -94
    Baer                                               PR_REP                          70 07-6月 -94
    Higgins                                            AC_MGR                         110 07-6月 -94
    Gietz                                              AC_ACCOUNT                     110 07-6月 -94
    De Haan                                            AD_VP                           90 13-1月 -93
    Ernst                                              IT_PROG                         60 21-5月 -91
    Hunold                                             IT_PROG                         60 03-1月 -90
    Kochhar                                            AD_VP                           90 21-9月 -89
    Whalen                                             AD_ASST                         10 17-9月 -87
    King                                               AD_PRES                         90 17-6月 -87
    
    已选择107行。
    

    对化名排序

    我们可以基于运算出的数据进行排序

    SELECT employee_id, last_name, salary*12 annsal
    FROM employees
    ORDER BY annsal;
    

    输出如下

    EMPLOYEE_ID LAST_NAME                                              ANNSAL
    ----------- -------------------------------------------------- ----------
            132 Olson                                                   25200
            128 Markle                                                  26400
            136 Philtanker                                              26400
            135 Gee                                                     28800
            127 Landry                                                  28800
            119 Colmenares                                              30000
            131 Marlow                                                  30000
            140 Patel                                                   30000
            144 Vargas                                                  30000
            182 Sullivan                                                30000
            191 Perkins                                                 30000
            143 Matos                                                   31200
            199 Grant                                                   31200
            118 Himuro                                                  31200
            198 OConnell                                                31200
            139 Seo                                                     32400
            126 Mikkilineni                                             32400
            130 Atkinson                                                33600
            183 Geoni                                                   33600
            117 Tobias                                                  33600
            195 Jones                                                   33600
            134 Rogers                                                  34800
            116 Baida                                                   34800
            190 Gates                                                   34800
            197 Feeney                                                  36000
            187 Cabrio                                                  36000
            181 Fleaur                                                  37200
            115 Khoo                                                    37200
            142 Davies                                                  37200
            196 Walsh                                                   37200
            194 McCain                                                  38400
            125 Nayer                                                   38400
            138 Stiles                                                  38400
            180 Taylor                                                  38400
            133 Mallin                                                  39600
            129 Bissot                                                  39600
            186 Dellinger                                               40800
            141 Rajs                                                    42000
            189 Dilly                                                   43200
            137 Ladwig                                                  43200
            188 Chung                                                   45600
            193 Everett                                                 46800
            192 Bell                                                    48000
            185 Bull                                                    49200
            107 Lorentz                                                 50400
            184 Sarchand                                                50400
            200 Whalen                                                  52800
            106 Pataballa                                               57600
            105 Austin                                                  57600
            124 Mourgos                                                 69600
            104 Ernst                                                   72000
            202 Fay                                                     72000
            173 Kumar                                                   73200
            167 Banda                                                   74400
            179 Johnson                                                 74400
            166 Ande                                                    76800
            123 Vollman                                                 78000
            203 Mavris                                                  78000
            165 Lee                                                     81600
            113 Popp                                                    82800
            155 Tuvault                                                 84000
            161 Sewall                                                  84000
            178 Grant                                                   84000
            164 Marvins                                                 86400
            172 Bates                                                   87600
            171 Smith                                                   88800
            154 Cambrault                                               90000
            160 Doran                                                   90000
            111 Sciarra                                                 92400
            112 Urman                                                   93600
            122 Kaufling                                                94800
            159 Smith                                                   96000
            120 Weiss                                                   96000
            153 Olsen                                                   96000
            110 Chen                                                    98400
            121 Fripp                                                   98400
            206 Gietz                                                   99600
            177 Livingston                                             100800
            176 Taylor                                                 103200
            175 Hutton                                                 105600
            103 Hunold                                                 108000
            109 Faviet                                                 108000
            152 Hall                                                   108000
            158 McEwen                                                 108000
            157 Sully                                                  114000
            163 Greene                                                 114000
            151 Bernstein                                              114000
            170 Fox                                                    115200
            204 Baer                                                   120000
            169 Bloom                                                  120000
            156 King                                                   120000
            150 Tucker                                                 120000
            162 Vishney                                                126000
            149 Zlotkey                                                126000
            148 Cambrault                                              132000
            114 Raphaely                                               132000
            174 Abel                                                   132000
            168 Ozer                                                   138000
            205 Higgins                                                144000
            108 Greenberg                                              144000
            147 Errazuriz                                              144000
            201 Hartstein                                              156000
            146 Partners                                               162000
            145 Russell                                                168000
            102 De Haan                                                204000
            101 Kochhar                                                204000
            100 King                                                   288000
    
    已选择107行。
    

    多关键字排序

    在排序时,我们可以给排序添加多个关键字

    排序规则:先比较第一关键字,如第一关键字不同则按第一关键字进行,否则比较下一个关键字

    SELECT last_name, department_id, salary
    FROM employees
    ORDER BY department_id, salary DESC;
    

    输出如下:

    LAST_NAME                                          DEPARTMENT_ID     SALARY
    -------------------------------------------------- ------------- ----------
    Whalen                                                        10       4400
    Hartstein                                                     20      13000
    Fay                                                           20       6000
    Raphaely                                                      30      11000
    Khoo                                                          30       3100
    Baida                                                         30       2900
    Tobias                                                        30       2800
    Himuro                                                        30       2600
    Colmenares                                                    30       2500
    Mavris                                                        40       6500
    Fripp                                                         50       8200
    Weiss                                                         50       8000
    Kaufling                                                      50       7900
    Vollman                                                       50       6500
    Mourgos                                                       50       5800
    Sarchand                                                      50       4200
    Bull                                                          50       4100
    Bell                                                          50       4000
    Everett                                                       50       3900
    Chung                                                         50       3800
    Dilly                                                         50       3600
    Ladwig                                                        50       3600
    Rajs                                                          50       3500
    Dellinger                                                     50       3400
    Mallin                                                        50       3300
    Bissot                                                        50       3300
    Taylor                                                        50       3200
    Nayer                                                         50       3200
    Stiles                                                        50       3200
    McCain                                                        50       3200
    Davies                                                        50       3100
    Walsh                                                         50       3100
    Fleaur                                                        50       3100
    Cabrio                                                        50       3000
    Feeney                                                        50       3000
    Rogers                                                        50       2900
    Gates                                                         50       2900
    Atkinson                                                      50       2800
    Jones                                                         50       2800
    Geoni                                                         50       2800
    Mikkilineni                                                   50       2700
    Seo                                                           50       2700
    Matos                                                         50       2600
    OConnell                                                      50       2600
    Grant                                                         50       2600
    Perkins                                                       50       2500
    Marlow                                                        50       2500
    Sullivan                                                      50       2500
    Patel                                                         50       2500
    Vargas                                                        50       2500
    Gee                                                           50       2400
    Landry                                                        50       2400
    Markle                                                        50       2200
    Philtanker                                                    50       2200
    Olson                                                         50       2100
    Hunold                                                        60       9000
    Ernst                                                         60       6000
    Austin                                                        60       4800
    Pataballa                                                     60       4800
    Lorentz                                                       60       4200
    Baer                                                          70      10000
    Russell                                                       80      14000
    Partners                                                      80      13500
    Errazuriz                                                     80      12000
    Ozer                                                          80      11500
    Abel                                                          80      11000
    Cambrault                                                     80      11000
    Zlotkey                                                       80      10500
    Vishney                                                       80      10500
    Bloom                                                         80      10000
    Tucker                                                        80      10000
    King                                                          80      10000
    Fox                                                           80       9600
    Bernstein                                                     80       9500
    Sully                                                         80       9500
    Greene                                                        80       9500
    Hall                                                          80       9000
    McEwen                                                        80       9000
    Hutton                                                        80       8800
    Taylor                                                        80       8600
    Livingston                                                    80       8400
    Olsen                                                         80       8000
    Smith                                                         80       8000
    Cambrault                                                     80       7500
    Doran                                                         80       7500
    Smith                                                         80       7400
    Bates                                                         80       7300
    Marvins                                                       80       7200
    Tuvault                                                       80       7000
    Sewall                                                        80       7000
    Lee                                                           80       6800
    Ande                                                          80       6400
    Johnson                                                       80       6200
    Banda                                                         80       6200
    Kumar                                                         80       6100
    King                                                          90      24000
    Kochhar                                                       90      17000
    De Haan                                                       90      17000
    Greenberg                                                    100      12000
    Faviet                                                       100       9000
    Chen                                                         100       8200
    Urman                                                        100       7800
    Sciarra                                                      100       7700
    Popp                                                         100       6900
    Higgins                                                      110      12000
    Gietz                                                        110       8300
    Grant                                                                  7000
    
    已选择107行。
    
  • 相关阅读:
    Bower 使用
    为什么是static?
    多重继承 -Javascript中的apply与call详解
    留用 未验证 js适配根字体大小
    Js作用域与作用域链详解
    理解AngularJS中的依赖注入
    渐进增强 优雅降级
    前后台数据交换的几种方式:
    then()方法是异步执行
    HTML怎么让img 等比例缩放
  • 原文地址:https://www.cnblogs.com/alphainf/p/16731946.html
Copyright © 2020-2023  润新知