SQL> create table student( 2 xh number(4), 3 xm varchar2(20), 4 sex char(2), 5 birthday date, 6 sal number(7,2) 7 ); Table created
SQL> create table class( 2 classid number(2), 3 cname varchar2(20) 4 ); Table created
SQL> alter table student add (classid number(2)); Table altered
SQL> alter table student modify (xm varchar2(30)); Table altered
SQL> alter table student modify(xm char(30)); Table altered
SQL> alter table student rename column xm to mc; Table altered
SQL> alter table student drop column sal; Table altered
SQL> rename student to stu; Table renamed
SQL> drop table stu; Table dropped
SQL> desc emp; Name Type Nullable Default Comments -------- ------------ -------- ------- -------- EMPNO NUMBER(4) ENAME VARCHAR2(10) Y JOB VARCHAR2(9) Y MGR NUMBER(4) Y HIREDATE DATE Y SAL NUMBER(7,2) Y COMM NUMBER(7,2) Y DEPTNO NUMBER(2) Y
SQL> select ename "姓名",sal*12 "年工资" from emp; 姓名 年工资 ---------- ---------- SMITH 21600 ALLEN 19200 WARD 15000 JONES 35700 MARTIN 15000 BLAKE 34200 CLARK 29400 SCOTT 36000 KING 60000 TURNER 18000 ADAMS 13200 JAMES 11400 FORD 36000 MILLER 15600 14 rows selected注:用nvl将null转化为0
SQL> select ename "姓名",sal*12+nvl(comm,0)*12 "年工资" from emp; 姓名 年工资 ---------- ---------- SMITH 21600 ALLEN 22800 WARD 21000 JONES 35700 MARTIN 31800 BLAKE 34200 CLARK 29400 SCOTT 36000 KING 60000 TURNER 18000 ADAMS 13200 JAMES 11400 FORD 36000 MILLER 16014.72 14 rows selected
SQL> select ename,sal from emp where ename like '__O%'; ENAME SAL ---------- --------- SCOTT 3000.00
SQL> select avg(sal) "平均工资",max(sal) "最高工资",deptno "部门编号" from emp group by deptno; 平均工资 最高工资 部门编号 ---------- ---------- -------- 1566.66666 2850 30 2375 3000 20 2916.66666 5000 10
SQL> select avg(sal) "平均工资",min(sal) "最低工资",deptno,job from emp group by deptno,job order by deptno; 平均工资 最低工资 DEPTNO JOB ---------- ---------- ------ --------- 1300 1300 10 CLERK 2450 2450 10 MANAGER 5000 5000 10 PRESIDENT 3000 3000 20 ANALYST 1450 1100 20 CLERK 2975 2975 20 MANAGER 950 950 30 CLERK 2850 2850 30 MANAGER 1400 1250 30 SALESMAN 9 rows selected
SQL> select deptno,avg(sal) "平均工资" from emp group by deptno having avg(sal)<2000; DEPTNO 平均工资 ------ ---------- 30 1566.66666
SQL> select ename from emp where deptno=(select deptno from emp where ename='SMITH'); ENAME ---------- SMITH JONES SCOTT ADAMS FORD
SQL> select ename,job,sal,deptno from emp where job in (select job from emp where deptno=10); ENAME JOB SAL DEPTNO ---------- --------- --------- ------ CLARK MANAGER 2450.00 10 BLAKE MANAGER 2850.00 30 JONES MANAGER 2975.00 20 KING PRESIDENT 5000.00 10 MILLER CLERK 1300.00 10 JAMES CLERK 950.00 30 ADAMS CLERK 1100.00 20 SMITH CLERK 1800.00 20 8 rows selected
SQL> select ename,sal,deptno from emp where sal>all(select sal from emp where deptno=30); ENAME SAL DEPTNO ---------- --------- ------ JONES 2975.00 20 SCOTT 3000.00 20 KING 5000.00 10 FORD 3000.00 20或者
SQL> select ename,sal,deptno from emp where sal>(select max(sal) from emp where deptno=30); ENAME SAL DEPTNO ---------- --------- ------ JONES 2975.00 20 SCOTT 3000.00 20 KING 5000.00 10 FORD 3000.00 20
SQL> select ename from emp where (deptno,job)=(select deptno,job from emp where ename='SMITH'); ENAME ---------- SMITH ADAMS
SQL> select ename,sal,svg "部门平均工资" from emp,(select deptno,avg(sal) svg from emp group by deptno) t where sal>svg and emp.deptno=t.deptno; ENAME SAL 部门平均工资 ---------- --------- ------------ ALLEN 1600.00 1566.6666666 JONES 2975.00 2375 BLAKE 2850.00 1566.6666666 SCOTT 3000.00 2375 KING 5000.00 2916.6666666 FORD 3000.00 2375 6 rows selected
SQL> update emp set (job,sal,comm)=(select job,sal,comm from emp where ename='SMITH') where ename='SCOTT'; 1 row updated
SQL> select * from emp where empno in(select empno from (select empno,rownum rn from (select empno from emp order by sal desc) where rownum<=10) t where t.rn>=6); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ---------- --------- ----- ----------- --------- --------- ------ 7782 CLARK MANAGER 7839 1981-06-09 2450.00 10 7844 TURNER SALESMAN 7698 1981-09-08 1500.00 0.00 30 7934 MILLER CLERK 7782 1982-01-23 1300.00 34.56 10 7499 ALLEN SALESMAN 7698 1981-02-20 1600.00 300.00 30 7369 SMITH CLERK 7902 1980-12-17 1800.00 20
SQL> create table emp1(id,name) as select empno,ename from emp; Table created Executed in 0.532 seconds SQL> select * from emp1; ID NAME ----- ---------- 7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD 7934 MILLER 14 rows selected
SQL> select empno id,ename name from emp 2 union 3 select id,name from emp1; ID NAME ----- ---------- 7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD 7934 MILLER 14 rows selected
SQL> select empno id,ename name from emp 2 union all 3 select id,name from emp1 4 ; ID NAME ----- ---------- 7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD 7934 MILLER 7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE ID NAME ----- ---------- 7782 CLARK 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD 7934 MILLER 28 rows selected
SQL> select ename,sal from emp where sal>2500 2 intersect 3 select ename,sal from emp where job='MANAGER'; ENAME SAL ---------- --------- BLAKE 2850.00 JONES 2975.00
SQL> select ename,sal from emp where sal>2500 2 minus 3 select ename,sal from emp where job='MANAGER' 4 ; ENAME SAL ---------- --------- FORD 3000.00 KING 5000.00 SCOTT 3000.00
原文地址:http://blog.csdn.net/mdcmy/article/details/41172679