Oracle例题(一)

备注:以下例题均基于Oracle的scott账户中的表

1、插入一条信息至SCOTT的EMP表

insert into emp values(7777,'AABB','PRESIDENT',7999, to_date('2017-11-1','YYYY-MM-DD'),10000,null,30);

2、查询首字母为"A"或者第二个字母为"A"的所有员工信息

select * from emp where ename like 'A%' or ename like '_A%';

select * from emp where substr(ename,1,1)='A' or substr(ename,2,1)='A';

select * from emp where instr(ename,'A')=1 or instr(ename,'A')=2;

3、查询部门20和30中的,岗位不是"CLERK"或"SALESMAN"的员工信息

select * from emp where job!='CLERK' and job!='SALESMAN' and (deptno = 20 or deptno = 30);//不好

select * from emp where job!='CLERK' and job!='SALESMAN' and deptno in(20,30);

4、查询出工资在2500-3500之间,1981年入职的没有奖金的员工的所有信息

select *
  from emp
 where sal between 2500 and 3500
   and to_char(hiredate, 'yyyy-mm-dd') like '1981%'
   and comm is null;

select *
  from emp
 where sal between 2500 and 3500
   and comm is null
   and extract(year from hiredate) = '1981';

5、查找比平均工资高的员工信息

select * from emp where sal > (select avg(sal) from emp);

6、查询平均工资高于2000的部门信息

select *
  from dept
 where deptno in
       (select deptno from emp group by deptno having avg(sal) > 2000);

7、查询出WARD的工作所在地

select loc
  from dept
 where deptno = (select deptno from emp where ename = 'WARD');

select dept.loc
  from dept, emp
 where dept.deptno = emp.deptno
   and emp.ename = 'WARD';

8、查询出工资比ADAMS高的所有人姓名、部门、所在地

select emp.ename, dept.dname, dept.loc
  from emp, dept
 where emp.deptno = dept.deptno
   and sal > (select sal from emp where ename = 'ADAMS');

9、查询工资排名第7的员工信息

select *
  from emp
 where sal = (select min(sal)
                from (select sal from emp order by sal DESC)
               where rownum <= 7);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 5.多表查询 多表查询 目的:从多张表获取数据 前提:进行连接的多张表中有共同的列 等连接 通过两个表具有相同意义...
    乔震阅读 1,330评论 0 0
  • oracle 数据库的scott帐号。 <>作为查询条件时,可以使用!= 来替换。 SQL> select * f...
    庄栋栋阅读 2,233评论 0 0
  • 5.DML DML 语句 语句操作语言 INSERT UPDATE DELETE MERGE INSERT 方法:...
    乔震阅读 1,023评论 0 0
  • SQL ==SQLPLUS== DML(Data Manipulation Language,数据操作语言)---...
    蝌蚪1573阅读 617评论 0 4
  • DC、老湿、王建国、马伯庸四人组成的镖局,接到大单,要求是将一个贴满封条的黑箱送至武林盟主的手中,只要将它完...
    小红袜阅读 242评论 3 1