分享

Oracle数据库面试练习题

 爱吃鱼的俊懒猫 2019-04-19

1.列出至少有一个员工的所有部门。
分析:每个部门有多少员工  —— 根据部门编号进行分组
select deptno,count(*) from emp group by deptno having count(*) >= 1;

2.列出薪金比“SMITH”多的所有员工。
分析:先查询出SMITH工资  : select sal from emp where ename=’SMITH';
select * from emp where sal > (select sal from emp where ename=’SMITH’);

3.***** 列出所有员工的姓名及其直接上级的姓名。
分析:表自映射,为表起别名,进行关联  t1 表模拟员工表 t2 表保存直接上级信息
select t1.ename 员工姓名, t2.ename 直接上级 from emp t1,emp t2 where t1.MGR = t2.empno;

4.列出受雇日期早于其直接上级的所有员工。
分析:原理和上题类似
select t1.*,t2.hiredate from emp t1,emp t2 where t1.MGR = t2.empno and t1.hiredate < t2.hiredate;

5.列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门。
分析:部门没员工也要显示 — 外连接。无论怎样部门信息一定要显示,通过部门去关联员工
select * from dept left outer join emp on dept.deptno = emp.deptno ;

6.列出所有“CLERK”(办事员)的姓名及其部门名称。
分析:查找job为CLERK 员工姓名和部门名称 
员工姓名 emp表
部门名称 dept表
select emp.ename,dept.dname,emp.job from emp,dept where emp.deptno = dept.deptno and emp.job=’CLERK';

7.列出最低薪金大于1500的各种工作。
分析:工作的最低薪金 —- 按工作分组,求最低薪金
select min(sal) from emp group by job;
大于1500 是一个分组条件 — having 
select job,min(sal) from emp group by job having min(sal) > 1500;

8.列出在部门“SALES”(销售部)工作的员工的姓名,假定不知道销售部的部门编号。
分析:员工姓名位于 emp  部门名称 dept
select emp.ename from emp,dept where emp.deptno = dept.deptno and dept.dname = ‘SALES';

9.列出薪金高于公司平均薪金的所有员工。
分析:先求公司平均薪金 select avg(sal) from emp;
select * from emp where sal > (select avg(sal) from emp);

10.列出与“SCOTT”从事相同工作的所有员工。
分析:先查询SCOTT : select job from emp where ename =’SCOTT';
select * from emp where ename <> ‘SCOTT’ and job = (select job from emp where ename =’SCOTT’);

11.列出薪金等于部门30中员工的薪金的所有员工的姓名和薪金。
分析:查看部门30 中所有员工薪资列表 select sal from emp where deptno = 30;
select * from emp where sal in (select sal from emp where deptno = 30);

12.列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。
分析:
select * from emp where sal > all(select sal from emp where deptno = 30);
select * from emp where sal > (select max(sal) from emp where deptno = 30);

13.列出在每个部门工作的员工数量、平均工资。
分析:按部门分组
select deptno, count(*),avg(sal)  from emp group by deptno;

14.列出所有员工的姓名、部门名称和工资。
分析:
select emp.ename,dept.dname,emp.sal from emp,dept where emp.deptno = dept.deptno;

15.列出所有部门的详细信息和部门人数。
分析:
select dept.*,count(emp.*) from emp,dept where emp.deptno = dept.deptno group by deptno ; 错误!
求各个部门编号和人数 select deptno,count(*) from emp group by deptno;
再和dept表关联 select dept.*,temp.部门人数 from dept , (select deptno,count(*) 部门人数 from emp group by deptno) temp where dept.deptno = temp.deptno ;

16.列出各种工作的最低工资。
分析:各个工作 分组 , 最低工资 min
select job,min(sal) from emp group by job;

17.列出各个部门的MANAGER(经理)的最低薪金。
分析:where job=’MANAGER’ 过滤所有不是经理数据
select deptno,min(sal) from emp where job =’MANAGER’ group by deptno;

18.列出所有员工的年工资,按年薪从低到高排序。 
分析: select ename, sal*12 from emp order by sal*12 asc;

19.查出emp表中薪水在3000以上(包括3000)的所有员工的员工号、姓名、薪水。
分析: select * from emp where sal >= 3000;

20.查询出所有薪水在’ALLEN’之上的所有人员信息。
分析:select * from emp where sal > (select sal from emp where ename =’ALLEN’);

21.查询出emp表中部门编号为20,薪水在2000以上(不包括2000)的所有员工,显示他们的员工号,姓名以及薪水,以如下列名显示:员工编号 员工名字 薪水
分析: select empno 员工编号,ename 员工姓名 ,sal 薪水 from emp where deptno = 20 and sal > 2000;

22.查询出emp表中所有的工作种类(无重复)
分析: select distinct job from emp;

23.查询出所有奖金(comm)字段不为空的人员的所有信息。
分析:不为空 is not null
select * from emp where comm is not null;

24.查询出薪水在800到2500之间(闭区间)所有员工的信息。(注:使用两种方式实现and以及between and)
分析:select * from emp where sal >= 800 and sal <= 2500;
select * from emp where sal between 800 and 2500;

25.查询出员工号为7521,7900,7782的所有员工的信息。(注:使用两种方式实现,or以及in)
分析:select * from emp where empno in(7521,7900,7782);
select * from emp where empno=7521 or empno = 7900 or empno = 7782;

26.查询出名字中有“A”字符,并且薪水在1000以上(不包括1000)的所有员工信息。
分析: 模糊查询
select * from emp where ename like ‘%A%’ and sal > 1000;

27.查询出名字第三个字母是“M”的所有员工信息。
分析:第三个字母 __M%
select * from emp where ename like ‘__M%';

28.将所有员工按薪水升序排序,薪水相同的按照入职时间降序排序。
分析:select * from emp order by sal asc,hiredate desc;

29.将所有员工按照名字首字母升序排序,首字母相同的按照薪水降序排序。
分析:SUBSTRING(‘字符串’,第几个字符,长度);  —- 首字母 substring(ename,1,1)
select * from emp order by substring(ename,1,1) asc,sal desc;

30.查询出最早工作的那个人的名字、入职时间和薪水。
分析:最早工作人 — hiredate 最小值
select ename,hiredate,sal from emp where hiredate = (select min(hiredate) from emp);
select ename,hiredate,sal from emp where hiredate <= all(select hiredate from emp);

> any === > min
> all === > max
< any === < max
< all === < min

31.显示所有员工的名字、薪水、奖金,如果没有奖金,暂时显示100.
分析:select ename,sal,comm from emp; —- 没有奖金显示100  函数ifnull
select ename,sal,ifnull(comm,100) from emp;

32.显示出薪水最高人的职位。
分析: select job from emp where sal = (select max(sal) from emp);
select job from emp where sal >= all(select sal from emp);

33.查出emp表中所有部门的最高薪水和最低薪水,部门编号为10的部门不显示。
分析:按部门分组 select deptno,max(sal),min(sal) from emp where deptno<>10 group by deptno;

34.删除10号部门薪水最高的员工。
分析:delete from emp where deptno=10 and sal >= all(select sal from emp where deptno=10 ); // MYSQL 不支持
Mysql 规范,修改或者删除 表中记录,不允许在子查询中 查询相同表
ERROR 1093 (HY000): You can’t specify target table ‘emp’ for update in FROM clause

解决方案:临时表
delete from emp where deptno=10 and sal >= all(select t.sal from (select sal from emp where deptno=10) t );

35.将薪水最高的员工的薪水降30%。
分析:update emp set sal = sal*0.7 where sal = (select max(sal) from emp); // MYSQL 不支持
引入 临时表
update emp set sal = sal*0.7 where sal = (select t.maxsal  from (select max(sal) maxsal from emp) t);

36.查询员工姓名,工资和 工资级别(工资>=3000 为3级,工资>2000 为2级,工资<=2000 为1级)
分析:
select ename,sal, case when sal>=3000 then ‘3级’ when sal>2000 then ‘2级’ else ‘1级’ end 级别 from emp;

语法:case … when … then … when … then … else … end

行列互换
姓名 课程 分数

张三 语文 74

张三 数学 83

张三 物理 93

李四 语文 74

李四 数学 84

李四 物理 94

想变成(得到如下结果):

姓名 语文 数学 物理

—- —- —- —-

李四 74   84   94

张三 74   83   93

——————-

select name,max(case when cource =’语文’ then score else 0 end) from scores group by name;

select name,max(case when cource =’语文’ then score else 0 end)  语文,max(case when cource =’数学’ then score else 0 end) 数学,
max(case when cource =’英语’ then score else 0 end) 英语  from scores group by name;

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约