标签:
SQL> /*
SQL> sql 优化2:
SQL> where条件优化:
SQL> 执行的顺序是从右往左去执行 where deptno = 10 and ename like ‘%a%‘; or
SQL> */
SQL> -- 排序 order by
SQL> -- 根据员工的sal 排序
SQL> select *
2 from emp
3 order by sal;
EMPNO ENAME JOB MGR HIREDATE
---------- -------------------- ------------------ ---------- --------------
SAL COMM DEPTNO
---------- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80
800 20
7900 JAMES CLERK 7698 03-12月-81
950 30
7876 ADAMS CLERK 7788 23-5月 -87
1100 20
EMPNO ENAME JOB MGR HIREDATE
---------- -------------------- ------------------ ---------- --------------
SAL COMM DEPTNO
---------- ---------- ----------
7521 WARD SALESMAN 7698 22-2月 -81
1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81
1250 1400 30
7934 MILLER CLERK 7782 23-1月 -82
1300 10
EMPNO ENAME JOB MGR HIREDATE
---------- -------------------- ------------------ ---------- --------------
SAL COMM DEPTNO
---------- ---------- ----------
7844 TURNER SALESMAN 7698 08-9月 -81
1500 0 30
7499 ALLEN SALESMAN 7698 20-2月 -81
1600 300 30
7782 CLARK MANAGER 7839 09-6月 -81
2450 10
EMPNO ENAME JOB MGR HIREDATE
---------- -------------------- ------------------ ---------- --------------
SAL COMM DEPTNO
---------- ---------- ----------
7698 BLAKE MANAGER 7839 01-5月 -81
2850 30
7566 JONES MANAGER 7839 02-4月 -81
2975 20
7788 SCOTT ANALYST 7566 19-4月 -87
3000 20
EMPNO ENAME JOB MGR HIREDATE
---------- -------------------- ------------------ ---------- --------------
SAL COMM DEPTNO
---------- ---------- ----------
7902 FORD ANALYST 7566 03-12月-81
3000 20
7839 KING PRESIDENT 17-11月-81
5000 10
已选择14行。
SQL> set linesize 200;
SQL> /
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 800 20
7900 JAMES CLERK 7698 03-12月-81 950 30
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7934 MILLER CLERK 7782 23-1月 -82 1300 10
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7566 JONES MANAGER 7839 02-4月 -81 2975 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7902 FORD ANALYST 7566 03-12月-81 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
已选择14行。
SQL> --默认情况下oarcle是按照升序排列
SQL> select *
2 from emp
3 order by sal desc;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7839 KING PRESIDENT 17-11月-81 5000 10
7902 FORD ANALYST 7566 03-12月-81 3000 20
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7934 MILLER CLERK 7782 23-1月 -82 1300 10
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7900 JAMES CLERK 7698 03-12月-81 950 30
7369 SMITH CLERK 7902 17-12月-80 800 20
已选择14行。
SQL> select *
2 from emp
3 order by sal asc;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 800 20
7900 JAMES CLERK 7698 03-12月-81 950 30
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7934 MILLER CLERK 7782 23-1月 -82 1300 10
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7566 JONES MANAGER 7839 02-4月 -81 2975 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7902 FORD ANALYST 7566 03-12月-81 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
已选择14行。
SQL> -- desc 降序 asc升序
SQL> -- 按照sal和部门号排序
SQL> select *
2 from emp
3 order by sal,deptno;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 800 20
7900 JAMES CLERK 7698 03-12月-81 950 30
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7934 MILLER CLERK 7782 23-1月 -82 1300 10
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7566 JONES MANAGER 7839 02-4月 -81 2975 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7902 FORD ANALYST 7566 03-12月-81 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
已选择14行。
SQL> select *
2 from emp
3 order by job,deptno;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7902 FORD ANALYST 7566 03-12月-81 3000 20
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7934 MILLER CLERK 7782 23-1月 -82 1300 10
7369 SMITH CLERK 7902 17-12月-80 800 20
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7900 JAMES CLERK 7698 03-12月-81 950 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7839 KING PRESIDENT 17-11月-81 5000 10
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
已选择14行。
SQL> -- 如果排序后面跟了多个列,那么会首先使用第一列来排,第一列拍出来的,使用第二列来拍,以此类推。
SQL> -- order by 后面能跟什么东西?
SQL> -- order by 列明,别名,表达式,列所在的数字顺序
SQL> select deptno,empno 员工号
2 from emp
3 order by 员工号;
DEPTNO 员工号
---------- ----------
20 7369
30 7499
30 7521
20 7566
30 7654
30 7698
10 7782
20 7788
10 7839
30 7844
20 7876
DEPTNO 员工号
---------- ----------
30 7900
20 7902
10 7934
已选择14行。
SQL> select sal*12 基本工资
2 from emp
3 order by sal*12;
基本工资
----------
9600
11400
13200
15000
15000
15600
18000
19200
29400
34200
35700
基本工资
----------
36000
36000
60000
已选择14行。
SQL> select empno,deptno,sal*12 + nvl(comm,0)
2 from emp
3 order by 3;
EMPNO DEPTNO SAL*12+NVL(COMM,0)
---------- ---------- ------------------
7369 20 9600
7900 30 11400
7876 20 13200
7521 30 15500
7934 10 15600
7654 30 16400
7844 30 18000
7499 30 19500
7782 10 29400
7698 30 34200
7566 20 35700
EMPNO DEPTNO SAL*12+NVL(COMM,0)
---------- ---------- ------------------
7788 20 36000
7902 20 36000
7839 10 60000
已选择14行。
SQL> select empno,deptno
2 from emp
3 order by empno,depton desc;
order by empno,depton desc
*
第 3 行出现错误:
ORA-00904: "DEPTON": 标识符无效
SQL> 3
3* order by empno,depton desc
SQL> c /depton/deptno;
3* order by empno,deptno desc
SQL> /
EMPNO DEPTNO
---------- ----------
7369 20
7499 30
7521 30
7566 20
7654 30
7698 30
7782 10
7788 20
7839 10
7844 30
7876 20
EMPNO DEPTNO
---------- ----------
7900 30
7902 20
7934 10
已选择14行。
SQL> select job,deptno
2 from emp
3 order by job,deptno desc;
JOB DEPTNO
------------------ ----------
ANALYST 20
ANALYST 20
CLERK 30
CLERK 20
CLERK 20
CLERK 10
MANAGER 30
MANAGER 20
MANAGER 10
PRESIDENT 10
SALESMAN 30
JOB DEPTNO
------------------ ----------
SALESMAN 30
SALESMAN 30
SALESMAN 30
已选择14行。
SQL> -- desc只用用于理他最近的那一列
SQL> select empno,comm
2 from emp
3 order by comm desc;
EMPNO COMM
---------- ----------
7369
7782
7902
7900
7876
7566
7698
7934
7788
7839
7654 1400
EMPNO COMM
---------- ----------
7521 500
7499 300
7844 0
已选择14行。
SQL> -- 在oracle中null最大
SQL> -- 让null在最后显示
SQL> select empno,comm
2 from emp
3 order by comm desc
4 nulls last;
EMPNO COMM
---------- ----------
7654 1400
7521 500
7499 300
7844 0
7788
7839
7876
7900
7902
7934
7698
EMPNO COMM
---------- ----------
7566
7369
7782
已选择14行。
SQL> spool off;
标签:
原文地址:http://www.cnblogs.com/877599949yan/p/5284994.html