《卸甲笔记》-限定查询与排序显示

限定查询与排序显示

限定查询指的是在数据查询时设置一系列的过滤条件,只有满足指定的条件后才可以进行显示

在SQL标准中限定查询的语法如下:

select [distinct] *|列名称 [as] [列别名],列名称 [as] [列别名], … from 表名称 [表别名] [where 条件(s)]

1查看emp表中的数据量

Oracle

SQL> select count(*) from emp;

COUNT(*)

----------

14

PPAS

scott=# select count(*) from emp;

count

-------

14

(1 row)

2 统计出基本工资高于1500元的全部雇员信息

Oracle

SQL> select * from emp where sal>1500;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7499 ALLEN          SALESMAN                 7698 20-FEB-81

1600       300             30

7566 JONES          MANAGER                 7839 02-APR-81

2975                    20

7698 BLAKE          MANAGER                 7839 01-MAY-81

2850                    30

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7782 CLARK          MANAGER                 7839 09-JUN-81

2450                    10

7788 SCOTT          ANALYST                    7566 19-APR-87

3000                    20

7839 KING           PRESIDENT                      17-NOV-81

5000                    10

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7902 FORD                    ANALYST                    7566 03-DEC-81

3000                    20

7 rows selected.

PPAS

scott=# select * from emp where sal>1500;

empno | ename |    job    | mgr  |      hiredate      |   sal   |  comm  | deptno

-------+-------+-----------+------+--------------------+---------+--------+--------

7499 | ALLEN | SALESMAN  | 7698 | 20-FEB-81 00:00:00 | 1600.00 | 300.00 |     30

7566 | JONES | MANAGER   | 7839 | 02-APR-81 00:00:00 | 2975.00 |        |     20

7698 | BLAKE | MANAGER   | 7839 | 01-MAY-81 00:00:00 | 2850.00 |        |     30

7782 | CLARK | MANAGER   | 7839 | 09-JUN-81 00:00:00 | 2450.00 |        |     10

7788 | SCOTT | ANALYST   | 7566 | 19-APR-87 00:00:00 | 3000.00 |        |     20

7839 | KING  | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |        |     10

7902 | FORD  | ANALYST   | 7566 | 03-DEC-81 00:00:00 | 3000.00 |        |     20

(7 rows)

3查询出所有基本工资小于等于2000元的全部雇员信息

Oracle

SQL> select * from emp where sal<=2000;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7369 SMITH          CLERK                         7902 17-DEC-80

800                    20

7499 ALLEN          SALESMAN                 7698 20-FEB-81

1600       300             30

7521 WARD                  SALESMAN                 7698 22-FEB-81

1250       500             30

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7654 MARTIN                SALESMAN                 7698 28-SEP-81

1250     1400             30

7844 TURNER                SALESMAN                 7698 08-SEP-81

1500         0             30

7876 ADAMS                 CLERK                         7788 23-MAY-87

1100                    20

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7900 JAMES          CLERK                         7698 03-DEC-81

950                    30

7934 MILLER                 CLERK                         7782 23-JAN-82

1300                    10

8 rows selected.

PPAS

scott=# select * from emp where sal<=2000;

empno | ename  |   job    | mgr  |      hiredate      |   sal   |  comm   | deptno

-------+--------+----------+------+--------------------+---------+---------+--------

7369 | SMITH  | CLERK    | 7902 | 17-DEC-80 00:00:00 |  800.00 |         |     20

7499 | ALLEN  | SALESMAN | 7698 | 20-FEB-81 00:00:00 | 1600.00 |  300.00 |     30

7521 | WARD   | SALESMAN | 7698 | 22-FEB-81 00:00:00 | 1250.00 |  500.00 |     30

7654 | MARTIN | SALESMAN | 7698 | 28-SEP-81 00:00:00 | 1250.00 | 1400.00 |     30

7844 | TURNER | SALESMAN | 7698 | 08-SEP-81 00:00:00 | 1500.00 |    0.00 |     30

7876 | ADAMS  | CLERK    | 7788 | 23-MAY-87 00:00:00 | 1100.00 |         |     20

7900 | JAMES  | CLERK    | 7698 | 03-DEC-81 00:00:00 |  950.00 |         |     30

7934 | MILLER | CLERK    | 7782 | 23-JAN-82 00:00:00 | 1300.00 |         |     10

(8 rows)

4根据之前的查询结果发现SMITH的工资 最低,所以现在希望可以取得SMITH的详细资料

Oracle

SQL> select * from emp where ename=‘SMITH‘;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7369 SMITH          CLERK                         7902 17-DEC-80

800                    20

PPAS

scott=# select * from emp where ename=‘SMITH‘;

empno | ename |  job  | mgr  |      hiredate      |  sal   | comm | deptno

-------+-------+-------+------+--------------------+--------+------+--------

7369 | SMITH | CLERK | 7902 | 17-DEC-80 00:00:00 | 800.00 |      |     20

(1 row)

5查询出所有业务员(CLERK)的雇员信息

Oracle

SQL>  select * from emp where job=‘CLERK‘;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7369 SMITH          CLERK                         7902 17-DEC-80

800                    20

7876 ADAMS                 CLERK                         7788 23-MAY-87

1100                    20

7900 JAMES          CLERK                         7698 03-DEC-81

950                    30

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7934 MILLER                 CLERK                         7782 23-JAN-82

1300                    10

PPAS

scott=# select * from emp where job=‘CLERK‘;

empno | ename  |  job  | mgr  |      hiredate      |   sal   | comm | deptno

-------+--------+-------+------+--------------------+---------+------+--------

7369 | SMITH  | CLERK | 7902 | 17-DEC-80 00:00:00 |  800.00 |      |     20

7876 | ADAMS  | CLERK | 7788 | 23-MAY-87 00:00:00 | 1100.00 |      |     20

7900 | JAMES  | CLERK | 7698 | 03-DEC-81 00:00:00 |  950.00 |      |     30

7934 | MILLER | CLERK | 7782 | 23-JAN-82 00:00:00 | 1300.00 |      |     10

(4 rows)

6为了和其他职位的雇员对比,现在决定查询所有不是业务员的雇员信息

Oracle

SQL>  select * from emp where job<>‘CLERK‘;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7499 ALLEN          SALESMAN                 7698 20-FEB-81

1600       300             30

7521 WARD                  SALESMAN                 7698 22-FEB-81

1250       500             30

7566 JONES          MANAGER                 7839 02-APR-81

2975                    20

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7654 MARTIN                SALESMAN                 7698 28-SEP-81

1250     1400             30

7698 BLAKE          MANAGER                 7839 01-MAY-81

2850                    30

7782 CLARK          MANAGER                 7839 09-JUN-81

2450                    10

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7788 SCOTT          ANALYST                    7566 19-APR-87

3000                    20

7839 KING           PRESIDENT                      17-NOV-81

5000                    10

7844 TURNER                SALESMAN                 7698 08-SEP-81

1500         0             30

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7902 FORD                    ANALYST                    7566 03-DEC-81

3000                    20

10 rows selected.

SQL> select * from emp where job!=‘CLERK‘;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7499 ALLEN          SALESMAN                 7698 20-FEB-81

1600       300             30

7521 WARD                  SALESMAN                 7698 22-FEB-81

1250       500             30

7566 JONES          MANAGER                 7839 02-APR-81

2975                    20

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7654 MARTIN                SALESMAN                 7698 28-SEP-81

1250     1400             30

7698 BLAKE          MANAGER                 7839 01-MAY-81

2850                    30

7782 CLARK          MANAGER                 7839 09-JUN-81

2450                    10

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7788 SCOTT          ANALYST                    7566 19-APR-87

3000                    20

7839 KING           PRESIDENT                      17-NOV-81

5000                    10

7844 TURNER                SALESMAN                 7698 08-SEP-81

1500         0             30

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7902 FORD                    ANALYST                    7566 03-DEC-81

3000                    20

10 rows selected.

PPAS

scott=# select * from emp where job<>‘CLERK‘;

empno | ename  |    job    | mgr  |      hiredate      |   sal   |  comm   | deptno

-------+--------+-----------+------+--------------------+---------+---------+--------

7499 | ALLEN  | SALESMAN  | 7698 | 20-FEB-81 00:00:00 | 1600.00 |  300.00 |     30

7521 | WARD   | SALESMAN  | 7698 | 22-FEB-81 00:00:00 | 1250.00 |  500.00 |     30

7566 | JONES  | MANAGER   | 7839 | 02-APR-81 00:00:00 | 2975.00 |         |     20

7654 | MARTIN | SALESMAN  | 7698 | 28-SEP-81 00:00:00 | 1250.00 | 1400.00 |     30

7698 | BLAKE  | MANAGER   | 7839 | 01-MAY-81 00:00:00 | 2850.00 |         |     30

7782 | CLARK  | MANAGER   | 7839 | 09-JUN-81 00:00:00 | 2450.00 |         |     10

7788 | SCOTT  | ANALYST   | 7566 | 19-APR-87 00:00:00 | 3000.00 |         |     20

7839 | KING   | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |         |     10

7844 | TURNER | SALESMAN  | 7698 | 08-SEP-81 00:00:00 | 1500.00 |    0.00 |     30

7902 | FORD   | ANALYST   | 7566 | 03-DEC-81 00:00:00 | 3000.00 |         |     20

(10 rows)

scott=# select * from emp where job!=‘CLERK‘;

empno | ename  |    job    | mgr  |      hiredate      |   sal   |  comm   | deptno

-------+--------+-----------+------+--------------------+---------+---------+--------

7499 | ALLEN  | SALESMAN  | 7698 | 20-FEB-81 00:00:00 | 1600.00 |  300.00 |     30

7521 | WARD   | SALESMAN  | 7698 | 22-FEB-81 00:00:00 | 1250.00 |  500.00 |     30

7566 | JONES  | MANAGER   | 7839 | 02-APR-81 00:00:00 | 2975.00 |         |     20

7654 | MARTIN | SALESMAN  | 7698 | 28-SEP-81 00:00:00 | 1250.00 | 1400.00 |     30

7698 | BLAKE  | MANAGER   | 7839 | 01-MAY-81 00:00:00 | 2850.00 |         |     30

7782 | CLARK  | MANAGER   | 7839 | 09-JUN-81 00:00:00 | 2450.00 |         |     10

7788 | SCOTT  | ANALYST   | 7566 | 19-APR-87 00:00:00 | 3000.00 |         |     20

7839 | KING   | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |         |     10

7844 | TURNER | SALESMAN  | 7698 | 08-SEP-81 00:00:00 | 1500.00 |    0.00 |     30

7902 | FORD   | ANALYST   | 7566 | 03-DEC-81 00:00:00 | 3000.00 |         |     20

(10 rows)

7查询出工资范围在1500~3000(包含1500和3000)元的全部雇员信息

Oracle

SQL>  select * from emp where sal>=1500 and sal<=3000;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7499 ALLEN          SALESMAN                 7698 20-FEB-81

1600       300             30

7566 JONES          MANAGER                 7839 02-APR-81

2975                    20

7698 BLAKE          MANAGER                 7839 01-MAY-81

2850                    30

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7782 CLARK          MANAGER                 7839 09-JUN-81

2450                    10

7788 SCOTT          ANALYST                    7566 19-APR-87

3000                    20

7844 TURNER                SALESMAN                 7698 08-SEP-81

1500         0             30

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7902 FORD                    ANALYST                    7566 03-DEC-81

3000                    20

7 rows selected.

PPAS

scott=# select * from emp where sal>=1500 and sal<=3000;

empno | ename  |   job    | mgr  |      hiredate      |   sal   |  comm  | deptno

-------+--------+----------+------+--------------------+---------+--------+--------

7499 | ALLEN  | SALESMAN | 7698 | 20-FEB-81 00:00:00 | 1600.00 | 300.00 |     30

7566 | JONES  | MANAGER  | 7839 | 02-APR-81 00:00:00 | 2975.00 |        |     20

7698 | BLAKE  | MANAGER  | 7839 | 01-MAY-81 00:00:00 | 2850.00 |        |     30

7782 | CLARK  | MANAGER  | 7839 | 09-JUN-81 00:00:00 | 2450.00 |        |     10

7788 | SCOTT  | ANALYST  | 7566 | 19-APR-87 00:00:00 | 3000.00 |        |     20

7844 | TURNER | SALESMAN | 7698 | 08-SEP-81 00:00:00 | 1500.00 |   0.00 |     30

7902 | FORD   | ANALYST  | 7566 | 03-DEC-81 00:00:00 | 3000.00 |        |     20

(7 rows)

8查询职位是销售人员,并且基本工资高于1200元的所有雇员信息

Oracle

SQL> select * from emp where job=‘SALESMAN‘ and sal>1200;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7499 ALLEN          SALESMAN                 7698 20-FEB-81

1600       300             30

7521 WARD                  SALESMAN                 7698 22-FEB-81

1250       500             30

7654 MARTIN                SALESMAN                 7698 28-SEP-81

1250     1400             30

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7844 TURNER                SALESMAN                 7698 08-SEP-81

1500         0             30

PPAS

scott=# select * from emp where job=‘SALESMAN‘ and sal>1200;

empno | ename  |   job    | mgr  |      hiredate      |   sal   |  comm   | deptno

-------+--------+----------+------+--------------------+---------+---------+--------

7499 | ALLEN  | SALESMAN | 7698 | 20-FEB-81 00:00:00 | 1600.00 |  300.00 |     30

7521 | WARD   | SALESMAN | 7698 | 22-FEB-81 00:00:00 | 1250.00 |  500.00 |     30

7654 | MARTIN | SALESMAN | 7698 | 28-SEP-81 00:00:00 | 1250.00 | 1400.00 |     30

7844 | TURNER | SALESMAN | 7698 | 08-SEP-81 00:00:00 | 1500.00 |    0.00 |     30

(4 rows)

9要求查询出10部门中的经理或者20部门中的业务员的信息

Oralce

SQL> select * from emp

2   where (deptno=10 and job=‘MANAGER‘) or (deptno=20 and job=‘CLERK‘);

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7369 SMITH          CLERK                         7902 17-DEC-80

800                    20

7782 CLARK          MANAGER                 7839 09-JUN-81

2450                    10

7876 ADAMS                 CLERK                         7788 23-MAY-87

1100                    20

PPAS

scott=# select * from emp

scott-# where (deptno=10 and job=‘MANAGER‘) or (deptno=20 and job=‘CLERK‘);

empno | ename |   job   | mgr  |      hiredate      |   sal   | comm | deptno

-------+-------+---------+------+--------------------+---------+------+--------

7369 | SMITH | CLERK   | 7902 | 17-DEC-80 00:00:00 |  800.00 |      |     20

7782 | CLARK | MANAGER | 7839 | 09-JUN-81 00:00:00 | 2450.00 |      |     10

7876 | ADAMS | CLERK   | 7788 | 23-MAY-87 00:00:00 | 1100.00 |      |     20

(3 rows)

10查询不是业务员且基本工资大于2000元的全部雇员信息

Oracle

SQL> select * from emp

2   where job !=‘CLERK‘ and sal>2000;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7566 JONES          MANAGER                 7839 02-APR-81

2975                    20

7698 BLAKE          MANAGER                 7839 01-MAY-81

2850                    30

7782 CLARK          MANAGER                 7839 09-JUN-81

2450                    10

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7788 SCOTT          ANALYST                    7566 19-APR-87

3000                    20

7839 KING           PRESIDENT                      17-NOV-81

5000                    10

7902 FORD                    ANALYST                    7566 03-DEC-81

3000                    20

6 rows selected.

SQL> select * from emp

2    where  job <>‘CLERK‘ and  sal>2000;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7566 JONES          MANAGER                 7839 02-APR-81

2975                    20

7698 BLAKE          MANAGER                 7839 01-MAY-81

2850                    30

7782 CLARK          MANAGER                 7839 09-JUN-81

2450                    10

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7788 SCOTT          ANALYST                    7566 19-APR-87

3000                    20

7839 KING           PRESIDENT                      17-NOV-81

5000                    10

7902 FORD                    ANALYST                    7566 03-DEC-81

3000                    20

6 rows selected.

SQL> select * from emp

2   where not(job=‘CLERK‘ or sal<=2000);

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7566 JONES          MANAGER                 7839 02-APR-81

2975                    20

7698 BLAKE          MANAGER                 7839 01-MAY-81

2850                    30

7782 CLARK          MANAGER                 7839 09-JUN-81

2450                    10

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7788 SCOTT          ANALYST                    7566 19-APR-87

3000                    20

7839 KING           PRESIDENT                      17-NOV-81

5000                    10

7902 FORD                    ANALYST                    7566 03-DEC-81

3000                    20

6 rows selected.

PPAS

scott=# select * from emp

scott-# where job !=‘CLERK‘ and sal>2000;

empno | ename |    job    | mgr  |      hiredate      |   sal   | comm | deptno

-------+-------+-----------+------+--------------------+---------+------+--------

7566 | JONES | MANAGER   | 7839 | 02-APR-81 00:00:00 | 2975.00 |      |     20

7698 | BLAKE | MANAGER   | 7839 | 01-MAY-81 00:00:00 | 2850.00 |      |     30

7782 | CLARK | MANAGER   | 7839 | 09-JUN-81 00:00:00 | 2450.00 |      |     10

7788 | SCOTT | ANALYST   | 7566 | 19-APR-87 00:00:00 | 3000.00 |      |     20

7839 | KING  | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |      |     10

7902 | FORD  | ANALYST   | 7566 | 03-DEC-81 00:00:00 | 3000.00 |      |     20

(6 rows)

scott=# select * from emp

scott-# where  job <>‘CLERK‘ and  sal>2000;

empno | ename |    job    | mgr  |      hiredate      |   sal   | comm | deptno

-------+-------+-----------+------+--------------------+---------+------+--------

7566 | JONES | MANAGER   | 7839 | 02-APR-81 00:00:00 | 2975.00 |      |     20

7698 | BLAKE | MANAGER   | 7839 | 01-MAY-81 00:00:00 | 2850.00 |      |     30

7782 | CLARK | MANAGER   | 7839 | 09-JUN-81 00:00:00 | 2450.00 |      |     10

7788 | SCOTT | ANALYST   | 7566 | 19-APR-87 00:00:00 | 3000.00 |      |     20

7839 | KING  | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |      |     10

7902 | FORD  | ANALYST   | 7566 | 03-DEC-81 00:00:00 | 3000.00 |      |     20

(6 rows)

scott=# select * from emp

scott-# where not(job=‘CLERK‘ or sal<=2000);

empno | ename |    job    | mgr  |      hiredate      |   sal   | comm | deptno

-------+-------+-----------+------+--------------------+---------+------+--------

7566 | JONES | MANAGER   | 7839 | 02-APR-81 00:00:00 | 2975.00 |      |     20

7698 | BLAKE | MANAGER   | 7839 | 01-MAY-81 00:00:00 | 2850.00 |      |     30

7782 | CLARK | MANAGER   | 7839 | 09-JUN-81 00:00:00 | 2450.00 |      |     10

7788 | SCOTT | ANALYST   | 7566 | 19-APR-87 00:00:00 | 3000.00 |      |     20

7839 | KING  | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |      |     10

7902 | FORD  | ANALYST   | 7566 | 03-DEC-81 00:00:00 | 3000.00 |      |     20

(6 rows)

范围查询:BETWEEN 最小值 AND 最大值

11使用between … and 操作符查询出工资范围在1500~3000(包括1500和3000元的全部雇员信息)

Oracle

SQL> select * from emp where sal between 1500 and 3000;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7499 ALLEN          SALESMAN                 7698 20-FEB-81

1600       300             30

7566 JONES          MANAGER                 7839 02-APR-81

2975                    20

7698 BLAKE          MANAGER                 7839 01-MAY-81

2850                    30

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7782 CLARK          MANAGER                 7839 09-JUN-81

2450                    10

7788 SCOTT          ANALYST                    7566 19-APR-87

3000                    20

7844 TURNER                SALESMAN                 7698 08-SEP-81

1500         0             30

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7902 FORD                    ANALYST                    7566 03-DEC-81

3000                    20

7 rows selected.

PPAS

scott=# select * from emp where sal between 1500 and 3000;

empno | ename  |   job    | mgr  |      hiredate      |   sal   |  comm  | deptno

-------+--------+----------+------+--------------------+---------+--------+--------

7499 | ALLEN  | SALESMAN | 7698 | 20-FEB-81 00:00:00 | 1600.00 | 300.00 |     30

7566 | JONES  | MANAGER  | 7839 | 02-APR-81 00:00:00 | 2975.00 |        |     20

7698 | BLAKE  | MANAGER  | 7839 | 01-MAY-81 00:00:00 | 2850.00 |        |     30

7782 | CLARK  | MANAGER  | 7839 | 09-JUN-81 00:00:00 | 2450.00 |        |     10

7788 | SCOTT  | ANALYST  | 7566 | 19-APR-87 00:00:00 | 3000.00 |        |     20

7844 | TURNER | SALESMAN | 7698 | 08-SEP-81 00:00:00 | 1500.00 |   0.00 |     30

7902 | FORD   | ANALYST  | 7566 | 03-DEC-81 00:00:00 | 3000.00 |        |     20

(7 rows)

12查询出在1981年雇佣的全部雇员信息

Oracle

SQL> select * from emp where hiredate between ‘01-Jan-1981‘ and ‘31-Dec-1981‘;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7499 ALLEN          SALESMAN                 7698 20-FEB-81

1600       300             30

7521 WARD                  SALESMAN                 7698 22-FEB-81

1250       500             30

7566 JONES          MANAGER                 7839 02-APR-81

2975                    20

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7654 MARTIN                SALESMAN                 7698 28-SEP-81

1250     1400             30

7698 BLAKE          MANAGER                 7839 01-MAY-81

2850                    30

7782 CLARK          MANAGER                 7839 09-JUN-81

2450                    10

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7839 KING           PRESIDENT                      17-NOV-81

5000                    10

7844 TURNER                SALESMAN                 7698 08-SEP-81

1500         0             30

7900 JAMES          CLERK                         7698 03-DEC-81

950                    30

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7902 FORD                    ANALYST                    7566 03-DEC-81

3000                    20

10 rows selected.

PPAS

scott=# select * from emp where hiredate between ‘01-1-1981‘ and ‘31-12-1981‘;

empno | ename  |    job    | mgr  |      hiredate      |   sal   |  comm   | deptno

-------+--------+-----------+------+--------------------+---------+---------+--------

7499 | ALLEN  | SALESMAN  | 7698 | 20-FEB-81 00:00:00 | 1600.00 |  300.00 |     30

7521 | WARD   | SALESMAN  | 7698 | 22-FEB-81 00:00:00 | 1250.00 |  500.00 |     30

7566 | JONES  | MANAGER   | 7839 | 02-APR-81 00:00:00 | 2975.00 |         |     20

7654 | MARTIN | SALESMAN  | 7698 | 28-SEP-81 00:00:00 | 1250.00 | 1400.00 |     30

7698 | BLAKE  | MANAGER   | 7839 | 01-MAY-81 00:00:00 | 2850.00 |         |     30

7782 | CLARK  | MANAGER   | 7839 | 09-JUN-81 00:00:00 | 2450.00 |         |     10

7839 | KING   | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |         |     10

7844 | TURNER | SALESMAN  | 7698 | 08-SEP-81 00:00:00 | 1500.00 |    0.00 |     30

7900 | JAMES  | CLERK     | 7698 | 03-DEC-81 00:00:00 |  950.00 |         |     30

7902 | FORD   | ANALYST   | 7566 | 03-DEC-81 00:00:00 | 3000.00 |         |     20

(10 rows)

判断内容是否为null:IS NULL 、IS NOT NULL

13 使用==进行NULL比较

Oracle

SQL> select * from emp where  comm=null and empno=7369;

no rows selected

PPAS

scott=# select * from emp where  comm=null and empno=7369;

empno | ename | job | mgr | hiredate | sal | comm | deptno

-------+-------+-----+-----+----------+-----+------+--------

(0 rows)

14查询出所有领取佣金的雇员的完整信息

Oracle

SQL>  select * from emp where comm is not null;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7499 ALLEN          SALESMAN                 7698 20-FEB-81

1600       300             30

7521 WARD                  SALESMAN                 7698 22-FEB-81

1250       500             30

7654 MARTIN                SALESMAN                 7698 28-SEP-81

1250     1400             30

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7844 TURNER                SALESMAN                 7698 08-SEP-81

1500         0             30

SQL>  select * from emp where not comm is null;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7499 ALLEN          SALESMAN                 7698 20-FEB-81

1600       300             30

7521 WARD                  SALESMAN                 7698 22-FEB-81

1250       500             30

7654 MARTIN                SALESMAN                 7698 28-SEP-81

1250     1400             30

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7844 TURNER                SALESMAN                 7698 08-SEP-81

1500         0             30

PPAS

scott=# select * from emp where comm is not null;

empno | ename  |   job    | mgr  |      hiredate      |   sal   |  comm   | deptno

-------+--------+----------+------+--------------------+---------+---------+--------

7499 | ALLEN  | SALESMAN | 7698 | 20-FEB-81 00:00:00 | 1600.00 |  300.00 |     30

7521 | WARD   | SALESMAN | 7698 | 22-FEB-81 00:00:00 | 1250.00 |  500.00 |     30

7654 | MARTIN | SALESMAN | 7698 | 28-SEP-81 00:00:00 | 1250.00 | 1400.00 |     30

7844 | TURNER | SALESMAN | 7698 | 08-SEP-81 00:00:00 | 1500.00 |    0.00 |     30

(4 rows)

scott=# select * from emp where not comm is null;

empno | ename  |   job    | mgr  |      hiredate      |   sal   |  comm   | deptno

-------+--------+----------+------+--------------------+---------+---------+--------

7499 | ALLEN  | SALESMAN | 7698 | 20-FEB-81 00:00:00 | 1600.00 |  300.00 |     30

7521 | WARD   | SALESMAN | 7698 | 22-FEB-81 00:00:00 | 1250.00 |  500.00 |     30

7654 | MARTIN | SALESMAN | 7698 | 28-SEP-81 00:00:00 | 1250.00 | 1400.00 |     30

7844 | TURNER | SALESMAN | 7698 | 08-SEP-81 00:00:00 | 1500.00 |    0.00 |     30

(4 rows)

15  查询所有不领取佣金的雇员的完整信息

Oracle

SQL> select * from emp where comm is null;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7369 SMITH          CLERK                         7902 17-DEC-80

800                    20

7566 JONES          MANAGER                 7839 02-APR-81

2975                    20

7698 BLAKE          MANAGER                 7839 01-MAY-81

2850                    30

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7782 CLARK          MANAGER                 7839 09-JUN-81

2450                    10

7788 SCOTT          ANALYST                    7566 19-APR-87

3000                    20

7839 KING           PRESIDENT                      17-NOV-81

5000                    10

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7876 ADAMS                 CLERK                         7788 23-MAY-87

1100                    20

7900 JAMES          CLERK                         7698 03-DEC-81

950                    30

7902 FORD                    ANALYST                    7566 03-DEC-81

3000                    20

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7934 MILLER                 CLERK                         7782 23-JAN-82

1300                    10

10 rows selected.

PPAS

scott=# select * from emp where comm is null;

empno | ename  |    job    | mgr  |      hiredate      |   sal   | comm | deptno

-------+--------+-----------+------+--------------------+---------+------+--------

7369 | SMITH  | CLERK     | 7902 | 17-DEC-80 00:00:00 |  800.00 |      |     20

7566 | JONES  | MANAGER   | 7839 | 02-APR-81 00:00:00 | 2975.00 |      |     20

7698 | BLAKE  | MANAGER   | 7839 | 01-MAY-81 00:00:00 | 2850.00 |      |     30

7782 | CLARK  | MANAGER   | 7839 | 09-JUN-81 00:00:00 | 2450.00 |      |     10

7788 | SCOTT  | ANALYST   | 7566 | 19-APR-87 00:00:00 | 3000.00 |      |     20

7839 | KING   | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |      |     10

7876 | ADAMS  | CLERK     | 7788 | 23-MAY-87 00:00:00 | 1100.00 |      |     20

7900 | JAMES  | CLERK     | 7698 | 03-DEC-81 00:00:00 |  950.00 |      |     30

7902 | FORD   | ANALYST   | 7566 | 03-DEC-81 00:00:00 | 3000.00 |      |     20

7934 | MILLER | CLERK     | 7782 | 23-JAN-82 00:00:00 | 1300.00 |      |     10

(10 rows)

16列出所有不领取奖金,同时基本工资大于2000元的全部雇员信息

Oracle

SQL> select * from emp where comm is null and sal>2000;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7566 JONES          MANAGER                 7839 02-APR-81

2975                    20

7698 BLAKE          MANAGER                 7839 01-MAY-81

2850                    30

7782 CLARK          MANAGER                 7839 09-JUN-81

2450                    10

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7788 SCOTT          ANALYST                    7566 19-APR-87

3000                    20

7839 KING           PRESIDENT                      17-NOV-81

5000                    10

7902 FORD                    ANALYST                    7566 03-DEC-81

3000                    20

6 rows selected.

PPAS

scott=# select * from emp where comm is null and sal>2000;

empno | ename |    job    | mgr  |      hiredate      |   sal   | comm | deptno

-------+-------+-----------+------+--------------------+---------+------+--------

7566 | JONES | MANAGER   | 7839 | 02-APR-81 00:00:00 | 2975.00 |      |     20

7698 | BLAKE | MANAGER   | 7839 | 01-MAY-81 00:00:00 | 2850.00 |      |     30

7782 | CLARK | MANAGER   | 7839 | 09-JUN-81 00:00:00 | 2450.00 |      |     10

7788 | SCOTT | ANALYST   | 7566 | 19-APR-87 00:00:00 | 3000.00 |      |     20

7839 | KING  | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |      |     10

7902 | FORD  | ANALYST   | 7566 | 03-DEC-81 00:00:00 | 3000.00 |      |     20

(6 rows)

17 查找不收取佣金 或 收取的佣金低于100元的员工

Oracle

SQL>  select * from emp where comm is null or comm<100;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7369 SMITH          CLERK                         7902 17-DEC-80

800                    20

7566 JONES          MANAGER                 7839 02-APR-81

2975                    20

7698 BLAKE          MANAGER                 7839 01-MAY-81

2850                    30

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7782 CLARK          MANAGER                 7839 09-JUN-81

2450                    10

7788 SCOTT          ANALYST                    7566 19-APR-87

3000                    20

7839 KING           PRESIDENT                      17-NOV-81

5000                    10

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7844 TURNER                SALESMAN                 7698 08-SEP-81

1500         0             30

7876 ADAMS                 CLERK                         7788 23-MAY-87

1100                    20

7900 JAMES          CLERK                         7698 03-DEC-81

950                    30

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7902 FORD                    ANALYST                    7566 03-DEC-81

3000                    20

7934 MILLER                 CLERK                         7782 23-JAN-82

1300                    10

11 rows selected.

PPAS

scott=# select * from emp where comm is null or comm<100;

empno | ename  |    job    | mgr  |      hiredate      |   sal   | comm | deptno

-------+--------+-----------+------+--------------------+---------+------+--------

7369 | SMITH  | CLERK     | 7902 | 17-DEC-80 00:00:00 |  800.00 |      |     20

7566 | JONES  | MANAGER   | 7839 | 02-APR-81 00:00:00 | 2975.00 |      |     20

7698 | BLAKE  | MANAGER   | 7839 | 01-MAY-81 00:00:00 | 2850.00 |      |     30

7782 | CLARK  | MANAGER   | 7839 | 09-JUN-81 00:00:00 | 2450.00 |      |     10

7788 | SCOTT  | ANALYST   | 7566 | 19-APR-87 00:00:00 | 3000.00 |      |     20

7839 | KING   | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |      |     10

7844 | TURNER | SALESMAN  | 7698 | 08-SEP-81 00:00:00 | 1500.00 | 0.00 |     30

7876 | ADAMS  | CLERK     | 7788 | 23-MAY-87 00:00:00 | 1100.00 |      |     20

7900 | JAMES  | CLERK     | 7698 | 03-DEC-81 00:00:00 |  950.00 |      |     30

7902 | FORD   | ANALYST   | 7566 | 03-DEC-81 00:00:00 | 3000.00 |      |     20

7934 | MILLER | CLERK     | 7782 | 23-JAN-82 00:00:00 | 1300.00 |      |     10

(11 rows)

18查找收取佣金的员工的不同工作

Oracle

SQL>  select distinct job from emp where comm is not null;

JOB

---------

SALESMAN

PPAS

scott=# select distinct job from emp where comm is not null;

job

----------

SALESMAN

(1 row)

列表范围查找:IN 、NOT IN

19查询出雇员编号是7369、7788/7566的雇员信息

Oracle

SQL>  select * from emp where empno=7369 or empno=7788 or empno=7566;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7369 SMITH          CLERK                         7902 17-DEC-80

800                    20

7566 JONES          MANAGER                 7839 02-APR-81

2975                    20

7788 SCOTT          ANALYST                    7566 19-APR-87

3000                    20

PPAS

scott=# select * from emp where empno=7369 or empno=7788 or empno=7566;

empno | ename |   job   | mgr  |      hiredate      |   sal   | comm | deptno

-------+-------+---------+------+--------------------+---------+------+--------

7369 | SMITH | CLERK   | 7902 | 17-DEC-80 00:00:00 |  800.00 |      |     20

7566 | JONES | MANAGER | 7839 | 02-APR-81 00:00:00 | 2975.00 |      |     20

7788 | SCOTT | ANALYST | 7566 | 19-APR-87 00:00:00 | 3000.00 |      |     20

(3 rows)

20 通过IN操作符指定查询范围

Oracle

SQL>  select * from emp where empno IN(7369,7788,7566);

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7369 SMITH          CLERK                         7902 17-DEC-80

800                    20

7566 JONES          MANAGER                 7839 02-APR-81

2975                    20

7788 SCOTT          ANALYST                    7566 19-APR-87

3000                    20

PPAS

scott=# select * from emp where empno IN(7369,7788,7566);

empno | ename |   job   | mgr  |      hiredate      |   sal   | comm | deptno

-------+-------+---------+------+--------------------+---------+------+--------

7369 | SMITH | CLERK   | 7902 | 17-DEC-80 00:00:00 |  800.00 |      |     20

7566 | JONES | MANAGER | 7839 | 02-APR-81 00:00:00 | 2975.00 |      |     20

7788 | SCOTT | ANALYST | 7566 | 19-APR-87 00:00:00 | 3000.00 |      |     20

(3 rows)

21查询除了7369、7788、7566之外的雇员信息

Oracle

SQL> select * from emp where empno NOT IN(7369,7788,7566);

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7499 ALLEN          SALESMAN                 7698 20-FEB-81

1600       300             30

7521 WARD                  SALESMAN                 7698 22-FEB-81

1250       500             30

7654 MARTIN                SALESMAN                 7698 28-SEP-81

1250     1400             30

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7698 BLAKE          MANAGER                 7839 01-MAY-81

2850                    30

7782 CLARK          MANAGER                 7839 09-JUN-81

2450                    10

7839 KING           PRESIDENT                      17-NOV-81

5000                    10

EMPNO ENAME              JOB                      MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7844 TURNER                SALESMAN                 7698 08-SEP-81

1500         0             30

7876 ADAMS                 CLERK                         7788 23-MAY-87

1100                    20

7900 JAMES          CLERK                         7698 03-DEC-81

950                    30

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7902 FORD                    ANALYST                    7566 03-DEC-81

3000                    20

7934 MILLER                 CLERK                         7782 23-JAN-82

1300                    10

11 rows selected.

PPAS

scott=# select * from emp where empno NOT IN(7369,7788,7566);

empno | ename  |    job    | mgr  |      hiredate      |   sal   |  comm   | deptno

-------+--------+-----------+------+--------------------+---------+---------+--------

7499 | ALLEN  | SALESMAN  | 7698 | 20-FEB-81 00:00:00 | 1600.00 |  300.00 |     30

7521 | WARD   | SALESMAN  | 7698 | 22-FEB-81 00:00:00 | 1250.00 |  500.00 |     30

7654 | MARTIN | SALESMAN  | 7698 | 28-SEP-81 00:00:00 | 1250.00 | 1400.00 |     30

7698 | BLAKE  | MANAGER   | 7839 | 01-MAY-81 00:00:00 | 2850.00 |         |     30

7782 | CLARK  | MANAGER   | 7839 | 09-JUN-81 00:00:00 | 2450.00 |         |     10

7839 | KING   | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |         |     10

7844 | TURNER | SALESMAN  | 7698 | 08-SEP-81 00:00:00 | 1500.00 |    0.00 |     30

7876 | ADAMS  | CLERK     | 7788 | 23-MAY-87 00:00:00 | 1100.00 |         |     20

7900 | JAMES  | CLERK     | 7698 | 03-DEC-81 00:00:00 |  950.00 |         |     30

7902 | FORD   | ANALYST   | 7566 | 03-DEC-81 00:00:00 | 3000.00 |         |     20

7934 | MILLER | CLERK     | 7782 | 23-JAN-82 00:00:00 | 1300.00 |         |     10

(11 rows)

注意:在使用NOT IN操作符时列表不能有NULL

22在使用NOT IN操作符中 设置 null

Oracle

SQL> select * from emp where empno NOT IN(7369,7788,null);

no rows selected

PPAS

scott=# select * from emp where empno NOT IN(7369,7788,null);

empno | ename | job | mgr | hiredate | sal | comm | deptno

-------+-------+-----+-----+----------+-----+------+--------

(0 rows)

模糊查询:LIKE、NOT LIKE

23查询出雇员姓名是以S开头的全部雇员信息

Oracle

SQL> select * from emp where ename like ‘S%‘;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7369 SMITH          CLERK                         7902 17-DEC-80

800                    20

7788 SCOTT          ANALYST                    7566 19-APR-87

3000                    20

PPAS

scott=# select * from emp where ename like ‘S%‘;

empno | ename |   job   | mgr  |      hiredate      |   sal   | comm | deptno

-------+-------+---------+------+--------------------+---------+------+--------

7369 | SMITH | CLERK   | 7902 | 17-DEC-80 00:00:00 |  800.00 |      |     20

7788 | SCOTT | ANALYST | 7566 | 19-APR-87 00:00:00 | 3000.00 |      |     20

(2 rows)

24查询姓名的第2个字母是M的全部雇员信息

Oracle

SQL> select * from emp where ename like ‘_M%‘;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7369 SMITH          CLERK                         7902 17-DEC-80

800                    20

PPAS

scott=# select * from emp where ename like ‘_M%‘;

empno | ename |  job  | mgr  |      hiredate      |  sal   | comm | deptno

-------+-------+-------+------+--------------------+--------+------+--------

7369 | SMITH | CLERK | 7902 | 17-DEC-80 00:00:00 | 800.00 |      |     20

(1 row)

25查询姓名中任意位置包含字母F的雇员信息

Oracle

SQL> select * from emp where ename like ‘%F%‘;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7902 FORD                    ANALYST                    7566 03-DEC-81

3000                    20

PPAS

scott=# select * from emp where ename like ‘%F%‘;

empno | ename |   job   | mgr  |      hiredate      |   sal   | comm | deptno

-------+-------+---------+------+--------------------+---------+------+--------

7902 | FORD  | ANALYST | 7566 | 03-DEC-81 00:00:00 | 3000.00 |      |     20

(1 row)

26查询姓名长度为6或者超过6的雇员信息

Oracle

SQL> select * from emp where ename like ‘______%‘;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7654 MARTIN                SALESMAN                 7698 28-SEP-81

1250     1400             30

7844 TURNER                SALESMAN                 7698 08-SEP-81

1500         0             30

7934 MILLER                 CLERK                         7782 23-JAN-82

1300                    10

PPAS

scott=# select * from emp where ename like ‘______%‘;

empno | ename  |   job    | mgr  |      hiredate      |   sal   |  comm   | deptno

-------+--------+----------+------+--------------------+---------+---------+--------

7654 | MARTIN | SALESMAN | 7698 | 28-SEP-81 00:00:00 | 1250.00 | 1400.00 |     30

7844 | TURNER | SALESMAN | 7698 | 08-SEP-81 00:00:00 | 1500.00 |    0.00 |     30

7934 | MILLER | CLERK    | 7782 | 23-JAN-82 00:00:00 | 1300.00 |         |     10

(3 rows)

27查询出基本工资中包含 1或者在81年雇佣的全部雇员信息

Oracle

SQL> select * from emp where sal like ‘%1%‘ or hiredate like ‘%81%‘;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7499 ALLEN          SALESMAN                 7698 20-FEB-81

1600       300             30

7521 WARD                  SALESMAN                 7698 22-FEB-81

1250       500             30

7566 JONES          MANAGER                 7839 02-APR-81

2975                    20

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7654 MARTIN                SALESMAN                 7698 28-SEP-81

1250     1400             30

7698 BLAKE          MANAGER                 7839 01-MAY-81

2850                    30

7782 CLARK          MANAGER                 7839 09-JUN-81

2450                    10

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7839 KING           PRESIDENT                      17-NOV-81

5000                    10

7844 TURNER                SALESMAN                 7698 08-SEP-81

1500         0             30

7876 ADAMS                 CLERK                         7788 23-MAY-87

1100                    20

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7900 JAMES          CLERK                         7698 03-DEC-81

950                    30

7902 FORD                    ANALYST                    7566 03-DEC-81

3000                    20

7934 MILLER                 CLERK                         7782 23-JAN-82

1300                    10

12 rows selected.

PPAS

scott=# select * from emp where sal like ‘%1%‘ or hiredate like ‘%81%‘;

empno | ename  |    job    | mgr  |      hiredate      |   sal   |  comm   | deptno

-------+--------+-----------+------+--------------------+---------+---------+--------

7499 | ALLEN  | SALESMAN  | 7698 | 20-FEB-81 00:00:00 | 1600.00 |  300.00 |     30

7521 | WARD   | SALESMAN  | 7698 | 22-FEB-81 00:00:00 | 1250.00 |  500.00 |     30

7566 | JONES  | MANAGER   | 7839 | 02-APR-81 00:00:00 | 2975.00 |         |     20

7654 | MARTIN | SALESMAN  | 7698 | 28-SEP-81 00:00:00 | 1250.00 | 1400.00 |     30

7698 | BLAKE  | MANAGER   | 7839 | 01-MAY-81 00:00:00 | 2850.00 |         |     30

7782 | CLARK  | MANAGER   | 7839 | 09-JUN-81 00:00:00 | 2450.00 |         |     10

7839 | KING   | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |         |     10

7844 | TURNER | SALESMAN  | 7698 | 08-SEP-81 00:00:00 | 1500.00 |    0.00 |     30

7876 | ADAMS  | CLERK     | 7788 | 23-MAY-87 00:00:00 | 1100.00 |         |     20

7900 | JAMES  | CLERK     | 7698 | 03-DEC-81 00:00:00 |  950.00 |         |     30

7902 | FORD   | ANALYST   | 7566 | 03-DEC-81 00:00:00 | 3000.00 |         |     20

7934 | MILLER | CLERK     | 7782 | 23-JAN-82 00:00:00 | 1300.00 |         |     10

(12 rows)

28不设置查询关键字标识查询全部

Oracle

SQL> select * from emp where empno like ‘%%‘ or ename like ‘%%‘ or job like ‘%%‘

2   or hiredate like ‘%%‘ or sal like ‘%%‘ or comm like ‘%%‘;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7369 SMITH          CLERK                         7902 17-DEC-80

800                    20

7499 ALLEN          SALESMAN                 7698 20-FEB-81

1600       300             30

7521 WARD                  SALESMAN                 7698 22-FEB-81

1250       500             30

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7566 JONES          MANAGER                 7839 02-APR-81

2975                    20

7654 MARTIN                SALESMAN                 7698 28-SEP-81

1250     1400             30

7698 BLAKE          MANAGER                 7839 01-MAY-81

2850                    30

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7782 CLARK          MANAGER                 7839 09-JUN-81

2450                    10

7788 SCOTT          ANALYST                    7566 19-APR-87

3000                    20

7839 KING           PRESIDENT                      17-NOV-81

5000                    10

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7844 TURNER                SALESMAN                 7698 08-SEP-81

1500         0             30

7876 ADAMS                 CLERK                         7788 23-MAY-87

1100                    20

7900 JAMES          CLERK                         7698 03-DEC-81

950                    30

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7902 FORD                    ANALYST                    7566 03-DEC-81

3000                    20

7934 MILLER                 CLERK                         7782 23-JAN-82

1300                    10

14 rows selected.

PPAS

scott=# select * from emp where empno like ‘%%‘ or ename like ‘%%‘ or job like ‘%%‘

scott-# or hiredate like ‘%%‘ or sal like ‘%%‘ or comm like ‘%%‘;

empno | ename  |    job    | mgr  |      hiredate      |   sal   |  comm   | deptno

-------+--------+-----------+------+--------------------+---------+---------+--------

7369 | SMITH  | CLERK     | 7902 | 17-DEC-80 00:00:00 |  800.00 |         |     20

7499 | ALLEN  | SALESMAN  | 7698 | 20-FEB-81 00:00:00 | 1600.00 |  300.00 |     30

7521 | WARD   | SALESMAN  | 7698 | 22-FEB-81 00:00:00 | 1250.00 |  500.00 |     30

7566 | JONES  | MANAGER   | 7839 | 02-APR-81 00:00:00 | 2975.00 |         |     20

7654 | MARTIN | SALESMAN  | 7698 | 28-SEP-81 00:00:00 | 1250.00 | 1400.00 |     30

7698 | BLAKE  | MANAGER   | 7839 | 01-MAY-81 00:00:00 | 2850.00 |         |     30

7782 | CLARK  | MANAGER   | 7839 | 09-JUN-81 00:00:00 | 2450.00 |         |     10

7788 | SCOTT  | ANALYST   | 7566 | 19-APR-87 00:00:00 | 3000.00 |         |     20

7839 | KING   | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |         |     10

7844 | TURNER | SALESMAN  | 7698 | 08-SEP-81 00:00:00 | 1500.00 |    0.00 |     30

7876 | ADAMS  | CLERK     | 7788 | 23-MAY-87 00:00:00 | 1100.00 |         |     20

7900 | JAMES  | CLERK     | 7698 | 03-DEC-81 00:00:00 |  950.00 |         |     30

7902 | FORD   | ANALYST   | 7566 | 03-DEC-81 00:00:00 | 3000.00 |         |     20

7934 | MILLER | CLERK     | 7782 | 23-JAN-82 00:00:00 | 1300.00 |         |     10

(14 rows)

29找出部门10中所有经理(MANAGER),部门20中所有业务员(CLERK),既不是经理又不是业务员但薪金大于2000元的所有员工的详细资料,并且要求 这些雇员的姓名中包含字母S或字母K

Oracle

SQL> select * from emp

2  where ((deptno=10 and job=‘MANAGER‘) or (deptno=20 and job=‘CLERK‘)

3  or (job not in (‘MANAGER‘,‘CLERK‘) and sal > 2000))

4  and (ename like ‘%S%‘ or ename like ‘%K%‘);

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7369 SMITH          CLERK                         7902 17-DEC-80

800                    20

7782 CLARK          MANAGER                 7839 09-JUN-81

2450                    10

7788 SCOTT          ANALYST                    7566 19-APR-87

3000                    20

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7839 KING           PRESIDENT                      17-NOV-81

5000                    10

7876 ADAMS                 CLERK                         7788 23-MAY-87

1100                   20

PPAS

scott=# select * from emp

scott-# where ((deptno=10 and job=‘MANAGER‘) or (deptno=20 and job=‘CLERK‘)

scott(# or (job not in (‘MANAGER‘,‘CLERK‘) and sal > 2000))

scott-# and (ename like ‘%S%‘ or ename like ‘%K%‘);

empno | ename |    job    | mgr  |      hiredate      |   sal   | comm | deptno

-------+-------+-----------+------+--------------------+---------+------+--------

7369 | SMITH | CLERK     | 7902 | 17-DEC-80 00:00:00 |  800.00 |      |     20

7782 | CLARK | MANAGER   | 7839 | 09-JUN-81 00:00:00 | 2450.00 |      |     10

7788 | SCOTT | ANALYST   | 7566 | 19-APR-87 00:00:00 | 3000.00 |      |     20

7839 | KING  | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |      |     10

7876 | ADAMS | CLERK     | 7788 | 23-MAY-87 00:00:00 | 1100.00 |      |     20

(5 rows)

对结果排序

30 查询员工的完整信息,并且按照级别工资由高到低进行排序

Oracle

SQL> select * from emp order by sal desc;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7839 KING           PRESIDENT                      17-NOV-81

5000                    10

7902 FORD                    ANALYST                    7566 03-DEC-81

3000                    20

7788 SCOTT          ANALYST                    7566 19-APR-87

3000                    20

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7566 JONES          MANAGER                 7839 02-APR-81

2975                    20

7698 BLAKE          MANAGER                 7839 01-MAY-81

2850                    30

7782 CLARK          MANAGER                 7839 09-JUN-81

2450                    10

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7499 ALLEN          SALESMAN                 7698 20-FEB-81

1600       300             30

7844 TURNER                SALESMAN                 7698 08-SEP-81

1500         0             30

7934 MILLER                 CLERK                         7782 23-JAN-82

1300                    10

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7521 WARD                  SALESMAN                 7698 22-FEB-81

1250       500             30

7654 MARTIN                SALESMAN                 7698 28-SEP-81

1250     1400             30

7876 ADAMS                 CLERK                         7788 23-MAY-87

1100                    20

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7900 JAMES          CLERK                         7698 03-DEC-81

950                    30

7369 SMITH          CLERK                         7902 17-DEC-80

800                    20

14 rows selected.

PPAS

scott=# select * from emp order by sal desc;

empno | ename  |    job    | mgr  |      hiredate      |   sal   |  comm   | deptno

-------+--------+-----------+------+--------------------+---------+---------+--------

7839 | KING   | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |         |     10

7902 | FORD   | ANALYST   | 7566 | 03-DEC-81 00:00:00 | 3000.00 |         |     20

7788 | SCOTT  | ANALYST   | 7566 | 19-APR-87 00:00:00 | 3000.00 |         |     20

7566 | JONES  | MANAGER   | 7839 | 02-APR-81 00:00:00 | 2975.00 |         |     20

7698 | BLAKE  | MANAGER   | 7839 | 01-MAY-81 00:00:00 | 2850.00 |         |     30

7782 | CLARK  | MANAGER   | 7839 | 09-JUN-81 00:00:00 | 2450.00 |         |     10

7499 | ALLEN  | SALESMAN  | 7698 | 20-FEB-81 00:00:00 | 1600.00 |  300.00 |     30

7844 | TURNER | SALESMAN  | 7698 | 08-SEP-81 00:00:00 | 1500.00 |    0.00 |     30

7934 | MILLER | CLERK     | 7782 | 23-JAN-82 00:00:00 | 1300.00 |         |     10

7654 | MARTIN | SALESMAN  | 7698 | 28-SEP-81 00:00:00 | 1250.00 | 1400.00 |     30

7521 | WARD   | SALESMAN  | 7698 | 22-FEB-81 00:00:00 | 1250.00 |  500.00 |     30

7876 | ADAMS  | CLERK     | 7788 | 23-MAY-87 00:00:00 | 1100.00 |         |     20

7900 | JAMES  | CLERK     | 7698 | 03-DEC-81 00:00:00 |  950.00 |         |     30

7369 | SMITH  | CLERK     | 7902 | 17-DEC-80 00:00:00 |  800.00 |         |     20

(14 rows)

31利用序号设置排序列

Oracle

SQL>  select empno,ename,sal,job from emp order by 3 desc;

EMPNO ENAME                     SAL JOB

---------- -------------------- ---------- ------------------

7839 KING                 5000 PRESIDENT

7902 FORD                          3000 ANALYST

7788 SCOTT                3000 ANALYST

7566 JONES                2975 MANAGER

7698 BLAKE                2850 MANAGER

7782 CLARK                2450 MANAGER

7499 ALLEN                1600 SALESMAN

7844 TURNER                      1500 SALESMAN

7934 MILLER                       1300 CLERK

7521 WARD                        1250 SALESMAN

7654 MARTIN                      1250 SALESMAN

EMPNO ENAME                     SAL JOB

---------- -------------------- ---------- ------------------

7876 ADAMS                       1100 CLERK

7900 JAMES                 950 CLERK

7369 SMITH                 800 CLERK

14 rows selected.

PPAS

scott=# select empno,ename,sal,job from emp order by 3 desc;

empno | ename  |   sal   |    job

-------+--------+---------+-----------

7839 | KING   | 5000.00 | PRESIDENT

7902 | FORD   | 3000.00 | ANALYST

7788 | SCOTT  | 3000.00 | ANALYST

7566 | JONES  | 2975.00 | MANAGER

7698 | BLAKE  | 2850.00 | MANAGER

7782 | CLARK  | 2450.00 | MANAGER

7499 | ALLEN  | 1600.00 | SALESMAN

7844 | TURNER | 1500.00 | SALESMAN

7934 | MILLER | 1300.00 | CLERK

7654 | MARTIN | 1250.00 | SALESMAN

7521 | WARD   | 1250.00 | SALESMAN

7876 | ADAMS  | 1100.00 | CLERK

7900 | JAMES  |  950.00 | CLERK

7369 | SMITH  |  800.00 | CLERK

(14 rows)

32按照级别工资由低到高排序

Oracle

SQL>  select * from emp order by sal;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7369 SMITH          CLERK                         7902 17-DEC-80

800                    20

7900 JAMES          CLERK                         7698 03-DEC-81

950                    30

7876 ADAMS                 CLERK                         7788 23-MAY-87

1100                    20

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7521 WARD                  SALESMAN                 7698 22-FEB-81

1250       500             30

7654 MARTIN                SALESMAN                 7698 28-SEP-81

1250     1400             30

7934 MILLER                 CLERK                         7782 23-JAN-82

1300                    10

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7844 TURNER                SALESMAN                 7698 08-SEP-81

1500         0             30

7499 ALLEN          SALESMAN                 7698 20-FEB-81

1600       300             30

7782 CLARK          MANAGER                 7839 09-JUN-81

2450                    10

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7698 BLAKE          MANAGER                 7839 01-MAY-81

2850                    30

7566 JONES          MANAGER                 7839 02-APR-81

2975                    20

7788 SCOTT          ANALYST                    7566 19-APR-87

3000                    20

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7902 FORD                    ANALYST                    7566 03-DEC-81

3000                    20

7839 KING           PRESIDENT                      17-NOV-81

5000                    10

14 rows selected.

SQL> select * from emp order by sal asc;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7369 SMITH          CLERK                         7902 17-DEC-80

800                    20

7900 JAMES          CLERK                         7698 03-DEC-81

950                    30

7876 ADAMS                 CLERK                         7788 23-MAY-87

1100                    20

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7521 WARD                  SALESMAN                 7698 22-FEB-81

1250       500             30

7654 MARTIN                SALESMAN                 7698 28-SEP-81

1250     1400             30

7934 MILLER                 CLERK                         7782 23-JAN-82

1300                    10

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7844 TURNER                SALESMAN                 7698 08-SEP-81

1500         0             30

7499 ALLEN          SALESMAN                 7698 20-FEB-81

1600       300             30

7782 CLARK          MANAGER                 7839 09-JUN-81

2450                    10

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7698 BLAKE          MANAGER                 7839 01-MAY-81

2850                    30

7566 JONES          MANAGER                 7839 02-APR-81

2975                    20

7788 SCOTT          ANALYST                    7566 19-APR-87

3000                    20

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7902 FORD                    ANALYST                    7566 03-DEC-81

3000                    20

7839 KING           PRESIDENT                      17-NOV-81

5000                    10

14 rows selected.

PPAS

scott=# select * from emp order by sal;

empno | ename  |    job    | mgr  |      hiredate      |   sal   |  comm   | deptno

-------+--------+-----------+------+--------------------+---------+---------+--------

7369 | SMITH  | CLERK     | 7902 | 17-DEC-80 00:00:00 |  800.00 |         |     20

7900 | JAMES  | CLERK     | 7698 | 03-DEC-81 00:00:00 |  950.00 |         |     30

7876 | ADAMS  | CLERK     | 7788 | 23-MAY-87 00:00:00 | 1100.00 |         |     20

7521 | WARD   | SALESMAN  | 7698 | 22-FEB-81 00:00:00 | 1250.00 |  500.00 |     30

7654 | MARTIN | SALESMAN  | 7698 | 28-SEP-81 00:00:00 | 1250.00 | 1400.00 |     30

7934 | MILLER | CLERK     | 7782 | 23-JAN-82 00:00:00 | 1300.00 |         |     10

7844 | TURNER | SALESMAN  | 7698 | 08-SEP-81 00:00:00 | 1500.00 |    0.00 |     30

7499 | ALLEN  | SALESMAN  | 7698 | 20-FEB-81 00:00:00 | 1600.00 |  300.00 |     30

7782 | CLARK  | MANAGER   | 7839 | 09-JUN-81 00:00:00 | 2450.00 |         |     10

7698 | BLAKE  | MANAGER   | 7839 | 01-MAY-81 00:00:00 | 2850.00 |         |     30

7566 | JONES  | MANAGER   | 7839 | 02-APR-81 00:00:00 | 2975.00 |         |     20

7902 | FORD   | ANALYST   | 7566 | 03-DEC-81 00:00:00 | 3000.00 |         |     20

7788 | SCOTT  | ANALYST   | 7566 | 19-APR-87 00:00:00 | 3000.00 |         |     20

7839 | KING   | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |         |     10

(14 rows)

scott=# select * from emp order by sal asc;

empno | ename  |    job    | mgr  |      hiredate      |   sal   |  comm   | deptno

-------+--------+-----------+------+--------------------+---------+---------+--------

7369 | SMITH  | CLERK     | 7902 | 17-DEC-80 00:00:00 |  800.00 |         |     20

7900 | JAMES  | CLERK     | 7698 | 03-DEC-81 00:00:00 |  950.00 |         |     30

7876 | ADAMS  | CLERK     | 7788 | 23-MAY-87 00:00:00 | 1100.00 |         |     20

7521 | WARD   | SALESMAN  | 7698 | 22-FEB-81 00:00:00 | 1250.00 |  500.00 |     30

7654 | MARTIN | SALESMAN  | 7698 | 28-SEP-81 00:00:00 | 1250.00 | 1400.00 |     30

7934 | MILLER | CLERK     | 7782 | 23-JAN-82 00:00:00 | 1300.00 |         |     10

7844 | TURNER | SALESMAN  | 7698 | 08-SEP-81 00:00:00 | 1500.00 |    0.00 |     30

7499 | ALLEN  | SALESMAN  | 7698 | 20-FEB-81 00:00:00 | 1600.00 |  300.00 |     30

7782 | CLARK  | MANAGER   | 7839 | 09-JUN-81 00:00:00 | 2450.00 |         |     10

7698 | BLAKE  | MANAGER   | 7839 | 01-MAY-81 00:00:00 | 2850.00 |         |     30

7566 | JONES  | MANAGER   | 7839 | 02-APR-81 00:00:00 | 2975.00 |         |     20

7902 | FORD   | ANALYST   | 7566 | 03-DEC-81 00:00:00 | 3000.00 |         |     20

7788 | SCOTT  | ANALYST   | 7566 | 19-APR-87 00:00:00 | 3000.00 |         |     20

7839 | KING   | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |         |     10

(14 rows)

33查询出所有业务员(CLERK)的详细资料,并且按照基本工资由低到高排序

Oracle

SQL>  select * from emp where job=‘CLERK‘ order by sal;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7369 SMITH          CLERK                         7902 17-DEC-80

800                    20

7900 JAMES          CLERK                         7698 03-DEC-81

950                    30

7876 ADAMS                 CLERK                         7788 23-MAY-87

1100                    20

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7934 MILLER                 CLERK                         7782 23-JAN-82

1300                    10

PPAS

scott=# select * from emp where job=‘CLERK‘ order by sal;

empno | ename  |  job  | mgr  |      hiredate      |   sal   | comm | deptno

-------+--------+-------+------+--------------------+---------+------+--------

7369 | SMITH  | CLERK | 7902 | 17-DEC-80 00:00:00 |  800.00 |      |     20

7900 | JAMES  | CLERK | 7698 | 03-DEC-81 00:00:00 |  950.00 |      |     30

7876 | ADAMS  | CLERK | 7788 | 23-MAY-87 00:00:00 | 1100.00 |      |     20

7934 | MILLER | CLERK | 7782 | 23-JAN-82 00:00:00 | 1300.00 |      |     10

(4 rows)

34查询出所有雇员信息,要求按照基本工资由高到低排序,如果工资相等则按照雇佣日期由早到晚进行排序

Oracle

SQL> select * from emp order by sal desc,hiredate  asc;

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7839 KING           PRESIDENT                      17-NOV-81

5000                    10

7902 FORD                    ANALYST                    7566 03-DEC-81

3000                    20

7788 SCOTT          ANALYST                    7566 19-APR-87

3000                    20

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7566 JONES          MANAGER                 7839 02-APR-81

2975                    20

7698 BLAKE          MANAGER                 7839 01-MAY-81

2850                    30

7782 CLARK          MANAGER                 7839 09-JUN-81

2450                    10

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7499 ALLEN          SALESMAN                 7698 20-FEB-81

1600       300             30

7844 TURNER                SALESMAN                 7698 08-SEP-81

1500         0             30

7934 MILLER                 CLERK                         7782 23-JAN-82

1300                    10

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7521 WARD                  SALESMAN                 7698 22-FEB-81

1250       500             30

7654 MARTIN                SALESMAN                 7698 28-SEP-81

1250     1400             30

7876 ADAMS                 CLERK                         7788 23-MAY-87

1100                    20

EMPNO ENAME              JOB                       MGR HIREDATE

---------- -------------------- ------------------ ---------- ------------

SAL     COMM     DEPTNO

---------- ---------- ----------

7900 JAMES          CLERK                         7698 03-DEC-81

950                    30

7369 SMITH          CLERK                         7902 17-DEC-80

800                    20

14 rows selected.

PPAS

scott=# select * from emp order by sal desc,hiredate  asc;

empno | ename  |    job    | mgr  |      hiredate      |   sal   |  comm   | deptno

-------+--------+-----------+------+--------------------+---------+---------+--------

7839 | KING   | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |         |     10

7902 | FORD   | ANALYST   | 7566 | 03-DEC-81 00:00:00 | 3000.00 |         |     20

7788 | SCOTT  | ANALYST   | 7566 | 19-APR-87 00:00:00 | 3000.00 |         |     20

7566 | JONES  | MANAGER   | 7839 | 02-APR-81 00:00:00 | 2975.00 |         |     20

7698 | BLAKE  | MANAGER   | 7839 | 01-MAY-81 00:00:00 | 2850.00 |         |     30

7782 | CLARK  | MANAGER   | 7839 | 09-JUN-81 00:00:00 | 2450.00 |         |     10

7499 | ALLEN  | SALESMAN  | 7698 | 20-FEB-81 00:00:00 | 1600.00 |  300.00 |     30

7844 | TURNER | SALESMAN  | 7698 | 08-SEP-81 00:00:00 | 1500.00 |    0.00 |     30

7934 | MILLER | CLERK     | 7782 | 23-JAN-82 00:00:00 | 1300.00 |         |     10

7521 | WARD   | SALESMAN  | 7698 | 22-FEB-81 00:00:00 | 1250.00 |  500.00 |     30

7654 | MARTIN | SALESMAN  | 7698 | 28-SEP-81 00:00:00 | 1250.00 | 1400.00 |     30

7876 | ADAMS  | CLERK     | 7788 | 23-MAY-87 00:00:00 | 1100.00 |         |     20

7900 | JAMES  | CLERK     | 7698 | 03-DEC-81 00:00:00 |  950.00 |         |     30

7369 | SMITH  | CLERK     | 7902 | 17-DEC-80 00:00:00 |  800.00 |         |     20

(14 rows)

本连载博客主要探讨Oracle与PPAS(PostgreSQL)数据库的差异,以帮助更多读者了解如何实现数据库迁移!

时间: 2024-10-05 11:17:42

《卸甲笔记》-限定查询与排序显示的相关文章

限定查询与排序显示(章节摘要)

1,数据查询的标准语法. SELECT [DISTINCT] * | 列 [AS][别名],列[AS][别名],... FROM 表名称 [别名] [WHERE 限定条件(s)] [ORDER BY 排序字段 [ASC | DESC][,排序字段[ASC | DESC]...]]; 2,多个字句的执行顺序为FROM,WHERE,SELECT,ORDER BY,其中ORDER BY字句永远放在最后执行. 3,在使用限定查询时,所讲解的若干个限定条件为关系运算,逻辑运算,BETWEEN...AND,

sql 简单查询,限定查询及排序

SQL(structured Query Language  结构化查询语言)是一种数据库查询和程序设计语言,用于存取数据以及查询.更新和管理关系数据库系统. SQL语言包含4个部分: ※数据定义语言(DDL),例如:CREATE.DROP.ALTER等语句. ※数据操作语言(DML),例如:INSERT.UPDATE.DELETE语句. ※数据查询语言(DQL),例如:SELECT语句. ※数据控制语言(DCL),例如:GRANT.REVOKE.COMMIT.ROLLBACK等语句. 简单查询

《卸甲笔记》-多表查询之二

13增加右外连接,显示部门40的信息Oracle```SQL> select * 2 from emp e,dept d 3 where e.deptno(+)=d.deptno; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO DEPTNO DNAME LOC---------- ---------- ---------- ---------- ------------ ---------- ---------- ---------- --------

《卸甲笔记》-分组统计查询

统计函数对比 1查询出公司每个月支出的月工资总和 Oracle SQL> select SUM(sal) from emp; SUM(SAL) ---------- 29025 PPAS scott=# select SUM(sal) from emp; sum ---------- 29025.00 (1 row) 2查询出公司的最高工资,最低工资,平均工资 Oracle SQL> select AVG(sal),ROUND(AVG(sal),2),MAX(sal),MIN(sal) fro

《卸甲笔记》-多表查询之一

1查询emp表中数据量 14条数据Oracle```SQL> select count(*) from emp; COUNT(*)---------- 14```PPAS```scott=# select count(*) from emp; count ------- 14(1 row)```2查询dept表中的数据库 4条数据Oracle```SQL> select count(*) from dept; COUNT(*)---------- 4```PPAS```scott=# sele

《卸甲笔记》-子查询

1查询公司中工资最低的雇员的完整信息 Oracle SQL> select * 2  from emp 3  where sal=( 4  select MIN(sal) from emp); EMPNO ENAME      JOB            MGR HIREDATE                 SAL     COMM    DEPTNO ---------- ---------- ---------- ---------- ------------ ---------- -

&lt;&lt;卸甲笔记&gt;&gt;-Oracle线下迁移到PPAS

迁移原则 1.尽量保持Oracle与PPAS一致,这会使得日后应用程序迁移更为简单 2.迁移前检查PPAS中是否有同名帐号及同名的Schema a)如果有,建议考虑删除或改名 b)如果没有,先手工建立与Oracle同名的帐号及Schema 3.用MTK迁移时,配置文件中目标数据库建议使用与Oracle同名的帐号(如本案例中使用scott),这样可以保持目标数据库中所有表和对象的主属用户都和Oracle中一致 一 环境准备 Oracle环境 a)       IP:192.168.1.113,管理

Oracle笔记(四) 简单查询、限定查询、数据的排序

Oracle笔记(四) 简单查询.限定查询.数据的排序 一.简单查询 SQL(Structured Query Language) 结构化查询语言,是一种数据库查询和程序设计语言,用于存取数据以及查询.更新和管理关系数据库系统.ANSI(美国国家标准学会)声称,SQL是关系数据库管理系统的标准语言. Oracle数据库之所以发展的很好,主要也是因为Oracle是全世界最早采用SQL语句的数据库产品. SQL功能强大,概括起来,它可以分成以下几组: DML(Data Manipulation La

iOS之SQLite中的查询与排序 访问手机相册 向手机相册中存图

Day05 SQLite中的查询与排序 按查询结果排序:[[email protected][NSSortDescriptor sortDescriptorWithKey:@"age"ascending:yes]]; 设置查询条件: NSPredicate  *pre=nil; 1.比较运算符 > <  >=  <= == != pre=[NSPridicate [email protected]“age>40”]; 2. 范围运算符 IN  BETWEE