查询练习

创建表空间    scott_space
create tablespace scott_space datafile 'D:\JavaLife\oracle\scott_data.dbf' size 100m;

创建用户    scott/1
create user scott identified by 1 default tablespace scott_space account unlock;

授权
grant connect, resource to scott;


创建表并插入数据
CREATE TABLE DEPT
       (DEPTNO NUMBER(2) CONSTRAINT PK_DEPT PRIMARY KEY,
    DNAME VARCHAR2(14) ,
    LOC VARCHAR2(13) ) ;
CREATE TABLE EMP
       (EMPNO NUMBER(4) CONSTRAINT PK_EMP PRIMARY KEY,
    ENAME VARCHAR2(10),
    JOB VARCHAR2(9),
    MGR NUMBER(4),
    HIREDATE DATE,
    SAL NUMBER(7,2),
    COMM NUMBER(7,2),
    DEPTNO NUMBER(2) CONSTRAINT FK_DEPTNO REFERENCES DEPT);
INSERT INTO DEPT VALUES
    (10,'ACCOUNTING','NEW YORK');
INSERT INTO DEPT VALUES (20,'RESEARCH','DALLAS');
INSERT INTO DEPT VALUES
    (30,'SALES','CHICAGO');
INSERT INTO DEPT VALUES
    (40,'OPERATIONS','BOSTON');
INSERT INTO EMP VALUES
(7369,'SMITH','CLERK',7902,to_date('17-12-1980','dd-mm-yyyy'),800,NULL,20);
INSERT INTO EMP VALUES
(7499,'ALLEN','SALESMAN',7698,to_date('20-2-1981','dd-mm-yyyy'),1600,300,30);
INSERT INTO EMP VALUES
(7521,'WARD','SALESMAN',7698,to_date('22-2-1981','dd-mm-yyyy'),1250,500,30);
INSERT INTO EMP VALUES
(7566,'JONES','MANAGER',7839,to_date('2-4-1981','dd-mm-yyyy'),2975,NULL,20);
INSERT INTO EMP VALUES
(7654,'MARTIN','SALESMAN',7698,to_date('28-9-1981','dd-mm-yyyy'),1250,1400,30);
INSERT INTO EMP VALUES
(7698,'BLAKE','MANAGER',7839,to_date('1-5-1981','dd-mm-yyyy'),2850,NULL,30);
INSERT INTO EMP VALUES
(7782,'CLARK','MANAGER',7839,to_date('9-6-1981','dd-mm-yyyy'),2450,NULL,10);
INSERT INTO EMP VALUES
(7788,'SCOTT','ANALYST',7566,to_date('13-7-87','dd-mm-rr')-85,3000,NULL,20);
INSERT INTO EMP VALUES
(7839,'KING','PRESIDENT',NULL,to_date('17-11-1981','dd-mm-yyyy'),5000,NULL,10);
INSERT INTO EMP VALUES
(7844,'TURNER','SALESMAN',7698,to_date('8-9-1981','dd-mm-yyyy'),1500,0,30);
INSERT INTO EMP VALUES
(7876,'ADAMS','CLERK',7788,to_date('13-7-87', 'dd-mm-rr')-51,1100,NULL,20);
INSERT INTO EMP VALUES
(7900,'JAMES','CLERK',7698,to_date('3-12-1981','dd-mm-yyyy'),950,NULL,30);
INSERT INTO EMP VALUES
(7902,'FORD','ANALYST',7566,to_date('3-12-1981','dd-mm-yyyy'),3000,NULL,20);
INSERT INTO EMP VALUES
(7934,'MILLER','CLERK',7782,to_date('23-1-1982','dd-mm-yyyy'),1300,NULL,10);




简单查询

1.  选择部门30中的所有员工
select * from emp where deptno = 30;

2.  列出所有办事员的姓名、编号和部门编号
select ename,empno,deptno from emp WHERE job = 'CLERK';

3.  找出佣金高于薪金的员工
select * from emp  where comm > sal;

4.  找出佣金高于薪金60%的员工
select * from emp  where comm > (0.6)*sal;

5.  找出部门10中所有经理和部门20中所有办事员的详细资料
select * from emp where (deptno=10and job='MANAGER') or (deptno=20and job='CLERK');

6.  找出部门10中所有经理,部门20中所有办事员,既不是经理又不是办事员但薪金大于或等于2000的所有员工的详细资料 
select * from emp where (deptno=10and job='MANAGER') or (deptno=20and job='CLERK')or job not in ('MANAGER','CLERK') and sal >=2000;

7.  找出收取佣金的员工的不同工作
select distinct job from emp where comm is not null;

8.  找出不收取佣金或收取的佣金低于100的员工
select * from emp where comm is null or comm < 100;

9.  找出各月倒数第三天受雇的所有员工
select * from emp where last_day(hiredate)-hiredate=2;

10.    找出早于12年前受雇的员工
select * from emp where months_between(sysdate,hiredate)/12 >=12;

11. 以首字母大写的方式显示所有员工的姓名
select INITCAP(ENAME) from emp;

12. 显示正好为5个字符的员工姓名
select ENAME from emp where length(ename)=5;

13. 显示不带有R的员工姓名  www.2cto.com  
select ENAME from emp where ename not like'%R%';

14. 显示所有员工的前三个字符
select substr(ename,1,3) from emp ;

15. 显示所有员工的姓名,用a替换所有A
select replace(ename,'A','a') from emp ;

16. 显示满10年服务年限的员工的姓名和受雇日期
select ename ,hiredate from emp where months_between(sysdate,hiredate)/12>=10;

17. 显示员工的详细资料,按姓名排序
select * from emp orderby ename ;

18. 显示员工的姓名和受雇日期,根据其服务年限,将最老的员工排在最前面
select ename ,hiredate from emp orderby hiredate asc;

19. 显示所有员工的姓名、工作和薪金,按工作的降序排序,若工作相同按薪金排序
select ename,job,sal from emp orderby job desc ,sal;

20.显示所有员工姓名、加入公司的年份和月份,按受雇日期所在月排序,若月份相同则将最早年份的员工排在最前面。
select ename,to_char(hiredate,'YYYY/MM') from emp orderby to_char(hiredate,'MM'),to_char(hiredate,'yyyy') asc ;

21. 显示在一个月为30天的情况所有员工的日薪金,忽略余数
select  round(sal/30) from emp;  

22. 找出在任何年份的2月受聘的所有员工
select * from emp where to_char(hiredate ,'MM')=2 ;

23. 对于每个员工,显示其加入公司的天数
select ename,round(sysdate-hiredate) emp_date from emp;

24. 以年月日的方式显示所有员工的服务年限
select ename,to_char(hiredate,'YYYY')||'年'||to_char(hiredate,'MM')||'月'||to_char(hiredate,'DD')||'日'from emp;
 

 

 

posted @ 2015-10-10 20:06  病了?来串代码吧!  阅读(243)  评论(0编辑  收藏  举报