时间:2021-07-01 10:21:17 帮助过:5人阅读
select e.deptno,min(e.sal) from emp e group by e.deptno; select ee.ename, d.dname,ee.sal from emp ee ,dept d where ee.deptno = d.deptno and ee.sal=(select min(sal) from emp);
<1>按工作分组,分组条件最低工资大于1500 SELECT job,MIN(sal) FROM emp GROUP BY job HAVING MIN(sal)>1500; <2>根据求出来的工作来求员工数 SELECT e.job,COUNT(e.empno) FROM emp e WHERE e.job IN(SELECT job FROM emp GROUP BY job HAVING MIN(sal) > 1500) GROUP BY e.job;
<1>查询出公司的平均工资 select avg(sal) from emp; 2073.21428571429 <2> select e.ename,e.empno,e.deptno,m.ename as leader from emp e,emp m where e.sal>(select avg(sal)from emp) and e.mgr = m.empno <3> select t.sal,t.ename,d.dname,t.leader,s.grade from (select e.sal,e.ename,e.empno,e.deptno,m.ename as leader from emp e,emp m where e.sal>(select avg(sal)from emp) and e.mgr = m.empno(+)) t,dept d,salgrade s where t.deptno = d.deptno and t.sal between s.losal and s.hisal; 网上的答案: SELECT e.empno,e.ename,s.grade,m.empno leaderNo,m.ename as leaderName,d.deptno,d.dname,d.loc FROM emp e,dept d,emp m,salgrade s WHERE e.sal>(SELECT AVG(sal) FROM emp) AND e.deptno=d.deptno AND e.mgr=m.empno(+) AND e.sal BETWEEN s.losal AND s.hisal;
步骤:查询平均工资及其部门编号 select deptno, avg(sal) from emp group by deptno select e.deptno, avg(sal) avgsal from emp e,dept d where e.deptno = d.deptno group by e.deptno order by avgsal select a.deptno,a.avgsal from (select e.deptno, avg(sal) avgsal from emp e,dept d where e.deptno = d.deptno group by e.deptno order by avgsal) a where rownum = 1
方法一: select deptno, avg(sal) avgsal from emp group by deptno; select e.*,t.avgsal from emp e ,(select deptno, avg(sal) avgsal from emp group by deptno) t where e.deptno = t.deptno and e.sal > t.avgsal 方法二: select * from emp e where e.sal>(select avg(sal) from emp where e.deptno = deptno)
select count(distinct(mgr)), deptno from emp where mgr is not null group by deptno
create table emp1 as select * from emp; insert into emp1 select * from emp; commit; select a.rowid,a.* from emp1 a where a.rowid > (select min(b.rowid) from emp1 b --注意这里是组函数。如果写成了b.rowid 的话,就会报“ORA-01427单行子查询返回多个行”。 where a.empno = b.empno and a.ename = b.ename and a.job = b.job and nvl(a.mgr,1) = nvl(b.mgr,1) and a.hiredate = b.hiredate and a.sal = b.sal and nvl(a.comm,1) = nvl(b.comm,1) and a.deptno = b.deptno )
delete from emp1 a where a.rowid > (select min(b.rowid) from emp1 b where a.empno = b.empno and a.ename = b.ename and a.job = b.job and nvl(a.mgr,1) = nvl(b.mgr,1) and a.hiredate = b.hiredate and a.sal = b.sal and nvl(a.comm,1) = nvl(b.comm,1) and a.deptno = b.deptno )
方法一: select * from (select * from emp e order by sal desc) where rownum < 11 方法二:(百度) select * from (select a.*,row_number() over(order by sal desc) rn from emp a )where rn < 11
注意:这道题是求每个部门的,一想到的可能就是按部门编号分组(group by deptno )这样一来的话,后面的将会非常难写,反正我是没有写出来。 方法一:(使用ROW_NUMBER()进行排位分组 按照deptno分组) select * from (select e.*, row_number() over(partition by deptno order by sal desc)rn from emp e) where rn <=2 方法二:(这里的emp表要经过order by sal desc 先排序,直接写emp查询的数据将是错误的。这个方法可以针对empno不相等的情况)
select * from emp a where a.empno in (select b.empno from (select * from emp order by sal desc) b where b.deptno = a.deptno and rownum <=2 ) 方法三:(这个方法可以针对empno不相等的情况) select * from emp a where 2>(select count(*) from emp b where b.deptno = a.deptno and b.sal>a.sal) 方法四:(通用查询:MySQL也适用) SELECT * FROM emp t1 WHERE (SELECT COUNT(1) FROM emp t2 WHERE t2.deptno=t1.deptno AND t2.sal >= t1.sal) <=2 ORDER BY deptno ASC,sal DESC
select a.empno,a.ename,a.sal,b.empno 领导员工号,b.ename 领导姓名,b.sal 领导工资 from emp a ,emp b where a.sal>b.sal and a.mgr = b.empno;
这个主要涉及了两个函数rank() 和dense_rank() 结合over(XXX) 开窗函数 select e.ename,e.sal,rank() over(order by sal desc)rank_,dense_rank() over(order by sal desc)drank from emp e
select m.* from emp m where (select count(*) from emp where mgr = m.empno)>2 由此题可以提高一些难度如下
答案就是还是上面的答案喽。因为每个员工信息中包含了部门编号(deptno)顶多在根据deptno排序就可以了。
select d.* ,sum_sal from dept d, (select sum(sal) sum_sal,deptno from emp group by deptno ) e where d.deptno = e.deptno and e.sum_sal > (select avg(sum(sal)) from emp group by deptno )
方法一: select extract(month from hiredate) month ,count(*) from emp where extract(year from hiredate) = 1981 group by extract(month from hiredate) order by month;
方法一: select extract(year from hiredate) year ,count(*) from emp group by extract(year from hiredate) order by year
select dname from dept where deptno in(select deptno from emp group by deptno having count(deptno) >=1); 或者 select d.* from dept d, (select deptno ,count(deptno) cou from emp group by deptno)t where d.deptno = t.deptno and cou>=5
2016年6月3日17:36:48 未完待续......
Oracle经典SQL
标签: