Oracle经典练习题及标准答案
oracle经典练习sql
/*1、选择在部门30中员工的所有信息*/
select * from scott.emp where deptno = '30'
/*2、列出职位为(MANAGER)的员工的编号,姓名 */
select empno, ename from scott.emp where job = 'MANAGER'
/*3、找出奖金高于工资的员工*/
select * from scott.emp where comm > sal
/*4、找出每个员工奖金和工资的总和 */
select ename, sal + nvl(comm, 0) from scott.emp
/*5、找出部门10中的经理(MANAGER)和部门20中的普通员工(CLERK) */
select *
from scott.emp
where deptno = '10'
and job = 'MANAGER'
union
select *
from scott.emp
where job = 'CLERK'
and deptno = '20'
/*6、找出部门10中既不是经理也不是普通员工,而且工资大于等于2000的员工 */ select *
from scott.emp
where job != 'MANAGER'
and job != 'CLERK'
and sal > 2000
/*7、找出有奖金的员工的不同工作 */
select distinct(job) from scott.emp where comm is not null
/*8、找出没有奖金或者奖金低于500的员工*/
select *
from scott.emp
where comm is not null
and comm > 500
/*9、显示雇员姓名,根据其服务年限,将最老的雇员排在最前面 */
select ename
from scott.emp
order by (months_between(sysdate, hiredate) / 12) desc
select ename,hiredate from scott.emp order by hiredate
/*10、找出每个月倒数第三天受雇的员工*/
select * from scott.emp where hiredate = last_day(hiredate) - 2
/*11、分别用case和decode函数列出员工所在的部门,deptno=10显示'部门10',
deptno=20显示'部门20'
deptno=30显示'部门30'
deptno=40显示'部门40'
否则为'其他部门'*/
select ename,
case deptno
when 10 then
'部门10'
when 20 then
'部门20'
when 30 then
'部门30'
when 40 then
'部门40'
else
'其他部门'
end 工资情况
from scott.emp
select ename,
decode(deptno,
10,
'部门10',
20,
'部门20',
30,
'部门30',
40,
'部门40',
'其他部门') 工资情况
from scott.emp
/*12、分组统计各部门下工资>500的员工的平均工资*/
select avg(sal) from scott.emp where sal > 500 group by deptno
/*13、统计各部门下平均工资大于500的部门*/
select deptno from scott.emp group by deptno having avg(sal) > 500
/*14、算出部门30中得到最多奖金的员工奖金 */
select max(comm) from scott.emp where deptno = 30
/*15、算出部门30中得到最多奖金的员工姓名*/
select ename
from scott.emp
where deptno = 30
and comm = (select max(comm) from scott.emp where deptno = 30)
/*16、算出每个职位的员工数和最低工资*/
select count(ename), min(sal), job from scott.emp group by job
/*17、列出员工表中每个部门的员工数,和部门no */
select count(ename), deptno from scott.emp group by deptno
/*18、得到工资大于自己部门平均工资的员工信息*/
select *
from scott.emp e
where sal > (select avg(sal) from scott.emp where e.deptno = deptno)
select *
from scott.emp e1,
(select avg(sal) sals, deptno from scott.emp group by deptno) e2
where sal > sals
and e1.deptno = e2.deptno
/*19、分组统计每个部门下,每种职位的平均奖金(也要算没奖金的人)和总工资(包括奖金) */
select avg(nvl(comm,0)), sum(sal + nvl(comm, 0))
from scott.emp
group by deptno,job
/*20、笛卡尔集*/
select * from scott.emp, scott.dept
/*21、显示员工ID,名字,直属主管ID,名字*/
select empno,
ename,
mgr,
(select ename from scott.emp e1 where e1.empno = e2.mgr) 直属主管名字
from scott.emp e2
/*22、DEPT表按照部门跟EMP表左关联*/
select *
fromscott.dept, scott.emp
wherescott.dept.deptno = scott.emp.deptno(+)
/*23、使用此语句重复的内容不再显示了*/
select distinct (job) from scott.emp
/*24、重复的内容依然显示 */
select *
from scott.emp
UNION ALL
select * from scott.emp
/*23和24题和22题是一样的 */
/*25、只显示了两个表中彼此重复的记录。*/
select *
fromscott.dept, scott.emp
wherescott.dept.deptno(+) = scott.emp.deptno
/*26、只显示了两张表中的不同记录*/
select * from scott.emp union select * from scott.emp
minus
(select * from scott.emp intersect select * from scott.emp)
(select * from scott.emp minus select * from scott.emp)
union
(select * from scott.emp minus select * from scott.emp)
表结构相同先union 只能有 -
/*27、列出员工表中每个部门的员工数,和部门no */
select count(ename), deptno from scott.emp group by deptno
/*28、列出员工表中每个部门的员工数(员工数必须大于3),和部门名称*/ select count(deptno),
deptno,
(selectdname from scott.dept where scott.dept.deptno = e1.deptno)
from scott.emp e1
group by deptno having count(deptno)>3
/*29、找出工资比jones多的员工*/
select *
from scott.emp
where sal > (select sal from scott.emp where ename = 'JONES')
/*30、列出所有员工的姓名和其上级的姓名 */
select ename,
(select ename from scott.emp e1 where e1.empno = e2.mgr) 上级的姓名
from scott.emp e2
/*31、以职位分组,找出平均工资最高的两种职位 */
select job
from scott.emp
group by job
having avg(sal) in (select max(sal) from scott.emp group by job )
select job
from (select job, avg(sal)
from scott.emp
group by job
order by avg(sal) desc)
whererownum<= 2
最大的:
select max(max_sal)
from (select job, avg(sal) max_sal from scott.emp group by job)
/*32、查找出不在部门20,且比部门20中任何一个人工资都高的员工姓名、部门名称*/
select ename, dname
from scott.emp e1, scott.dept e2
where e1.deptno = e2.deptno
and e1.deptno <> 20
and sal > (select max(sal) from scott.emp where deptno = '20')
相关推荐:
- [初中教育]婚姻家庭法学教学教案
- [初中教育]浅谈小学语文教学中的创新教育
- [初中教育]中华人民共和国侵权责任法2009
- [初中教育]2016-2022年中国薄膜太阳能电池行业发
- [初中教育]多级轻型井点降水的应用
- [初中教育]外语教学法流派介绍和简评
- [初中教育]实验一、典型环节及其阶跃响应
- [初中教育]内蒙古2012-2013学年度国家奖学金获奖
- [初中教育]移动通信营销渠道管理探讨
- [初中教育]初三化学第一学期第一第二章基础知识点
- [初中教育]一天的食物教学设计
- [初中教育]光导照明系统的基本结构及工作原理
- [初中教育]长春市十一高、东北师范大学附属中学、
- [初中教育]“十三五”规划重点-配重式装卸车项目
- [初中教育]领导方法和领导艺术
- [初中教育]第三章 植物病虫草鼠害诊断与防治基
- [初中教育]2019届九年级语文上册 第二单元 6纪念
- [初中教育]甲级单位编制水豆腐项目可行性报告(立
- [初中教育]Ch8-1补充 09101数据库系统原理及应用-
- [初中教育]2017-2023年中国吊装设备行业市场分析
- 制作毕业纪念册需要哪些材料
- 2015-2016学年高二化学苏教版选修4课件
- 哈佛管理导师-创建商业案例
- 职场交际中的谈吐礼仪知识与职场会议接
- 中国糕点及面包行业发展现状与竞争战略
- 沂河“12·7”洪水茶山拦河坝
- 管道水流量计算公式
- 4-2发电机火灾事故处置方案
- 数字信号处理实验五
- 2009年经济师(中级)金融专业知识全真试
- 历史街区保护规划--04历史文化遗产保护
- 宁夏回族自治区中小学职称评价标准
- 评先评优测评表
- 圆的切线证明及线段长求解在在中考中的
- 【解析版】2015年江苏省南京外国语学校
- 人教版八年级上册科学第一章习题精华
- 责任心与执行力
- SA8000社会责任管理体系标准培训
- IgA肾病的饮食应注意
- 杭州市建设工程文件归档整理方案(试行)




