Oracle数据库面试练习题

发表于:2016-2-22 10:17

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:M-li    来源:51Testing软件测试网采编

分享:
  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;
22/2<12
精选软件测试好文,快来阅读吧~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号