设为首页 收藏本站
查看: 907|回复: 0

[经验分享] Oracle SQL的练习(转)

[复制链接]

尚未签到

发表于 2016-7-19 11:04:02 | 显示全部楼层 |阅读模式
--列出员工表中每个部门的员工数,和部门 no
select t.deptno,count(1) from scott.emp t group by t.deptno;
--列出员工表中每个部门的员工数(员工数必须大于 3),和部门名称
select t.deptno,t1.dname,count(1)
from scott.emp t
left join scott.dept t1 on t.deptno=t1.deptno
group by t.deptno,t1.dname
having count(1)>3;
--找出工资比 ward 多的员工
select a.* from scott.emp a, scott.emp b
where a.sal>b.sal and b.ename='ward';
--列出所有员工的姓名和其上级的姓名
select a.empno,a.ename,a.mgr,b.empno,b.ename from scott.emp a, scott.emp b
where a.mgr=b.empno;
--以职位分组,找出平均工资最高的两种职位
select * from (select job,avg(sal) from scott.emp group by job order by avg(sal) desc )where rownum<3;
--查找出不在部门 20,且比部门 20 中任何一个人工资都高的员工姓名、部门名称
--比任何人的工资高就是比工资最高的还要高
select * from scott.emp where sal>(select max(sal) from scott.emp where deptno='20') and deptno<>'20';
--得到平均工资大于 2000 的工作职种
select job from scott.emp group by job having avg(sal)>2000;
--分部门得到  工资大于 2000 的所有员工的平均工资,并且平均工资还要大于 2500
select t.deptno,avg(sal) from scott.emp t where t.sal>2000 group by t.deptno having avg(sal)>2500;
--得到每个月工资总数最少的那个部门的部门编号,部门名称,部门位置
select * from scott.dept
where deptno = (
select c.deptno from (select deptno,sum(sal) from scott.emp group by deptno order by sum(sal)) c
where rownum=1
);
--分部门得到平均工资等级为 3 级(等级表)的部门编号
select b.dno from scott.salgrade a,
(select t.deptno as dno,avg(t.sal) as avgsal
from scott.emp t
group by t.deptno) b
where a.grade=3 and b.avgsal between a.losal and a.hisal;
--查找出部门 10 和部门 20 中,工资最高第 3 名到工资第 5 名的员工的员工名字,部门名字,部门位置
select * from (select rownum no,b.* from (select a.* from scott.emp a where a.deptno in(10,20) order by a.sal desc) b) c
where c.no>=3 and c.no<=5;
select c.ename,d.dname,d.loc from (select rownum no,b.* from (select a.* from scott.emp a where a.deptno in(10,20) order by a.sal desc) b) c,scott.dept d
where c.deptno=d.deptno and c.no>=3 and c.no<=5;
--查找出收入(工资加上奖金),下级比自己上级还高的员工编号,员工名字,员工收入
select a.empno,a.ename,a.sal+nvl(a.comm,0) from scott.emp a,scott.emp b
where a.mgr=b.empno and (a.sal+nvl(a.comm,0))>(b.sal+nvl(b.comm,0));
--查找出工资等级不为 4 级的员工的员工名字,部门名字,部门位置
select c.ename,c.deptno,d.loc,c.sal from scott.dept d,
(select a.ename,a.deptno,a.sal from scott.emp a,scott.salgrade b
where b.grade<>4 and a.sal between b.losal and b.hisal) c
where d.deptno=c.deptno;
--查找出职位和'MARTIN'  或者'SMITH'一样的员工的平均工资
select avg(sal) from scott.emp where job in (select job from scott.emp where ename in('MARTIN','SMITH')) group by job;
--查找出不属于任何部门的员工
select * from scott.emp where deptno is null or deptno not in(select deptno from scott.dept);
--按部门统计员工数,查处员工数最多的部门的第二名到第五名(列出部门名字,部门位置)
select c.dname,c.loc from (select rownum no,a.* from(select t.deptno,count(1) from scott.emp t group by t.deptno order by count(1) desc) a)b ,scott.dept c
where b.deptno=c.deptno and b.no>=2 and b.no<=5;
--查询出 king 所在部门的部门号\部门名称\部门人数
select b.deptno,a.dname,b.count from scott.dept a,
(select deptno,count(1) as count from scott.emp
where deptno in(select deptno from scott.emp where ename = 'KING') group by deptno) b
where a.deptno=b.deptno;
select b.deptno,a.dname,b.count from scott.dept a,
(select deptno,t.count from(select deptno,count(1) as count from scott.emp  group by deptno) t
where t.deptno in(select deptno from scott.emp where ename = 'KING')) b
where a.deptno=b.deptno;
select a.deptno 部门号,a.dname 部门名称,(select count(*) from scott.emp where deptno in (select deptno from scott.emp where ename ='KING')) 部门人数  
from scott.dept a,scott.emp b where a.deptno=b.deptno and b.ename='KING';
--查询出 king 所在部门的工作年限最大的员工名字
select t.ename from(select rownum no,a.* from(select ename,hiredate from scott.emp where deptno in(select deptno from scott.emp where ename='KING') order by hiredate) a) t
where t.no=1;
--查询出工资成本最高的部门的部门号和部门名称
select deptno,dname from scott.dept where deptno in(
select b.deptno from (select rownum no,a.* from (select deptno,sum(sal) from scott.emp group by deptno order by sum(sal) desc) a) b where b.no=1 );

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-246262-1-1.html 上篇帖子: Oracle 中的游标用法 下篇帖子: 查看Oracle 触发器
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表