Orcale database exercises men will be silent when watching, women will cry when watching

– 1. The name of the person who got the highest salary in each department

– (1).in accepts parameters

select ename "姓名", sal "工资" , deptno "部门编号"
from emp 
where sal in(select max(sal)
from emp
group by deptno);

– (2) Internal connection

select  e.ename "姓名", e.sal "工资" , e.deptno "部门编号"
from emp e ,(
select deptno, max(sal) maxsal
from emp
group by deptno
)e1
where (e.deptno = e1.deptno) and(sal = e1.maxsal);

Insert picture description here

– 2. Get the average salary grade (of all people) in the department

select deptno ,avg(grade)"平均的薪水等级"
from (select e.empno, e.ename,e.deptno,s.grade
from emp e,salgrade s
where e.sal between s.losal and s.hisal)
group by deptno;

Insert picture description here

–3 The group function (Max) is not allowed to obtain the highest salary (two solutions are given)

-The first very simple method rownum

select *
from(
select * from emp order by sal desc)
where rownum=1;

-The second

select * 
from emp
where sal not in(
select distinct e1.sal
from emp e1,emp e2
where e1.sal<e2.sal);

Insert picture description here

– 4 The department number of the department with the highest average salary (at least two solutions are given)

-The first use rownum

select deptno "平均薪水最高的部门"
from(
select avg(sal),e.deptno
from emp e,dept d
where e.deptno = d.deptno
group by e.deptno
order by deptno asc)
where rownum =1;

– The second type:

Select deptno 
from emp 
group by deptno
having avg(sal) = (
select max(avg(sal)) from emp group by deptno)

Insert picture description here

-5. Who's salary is above the average salary of the department

select e1.ename "姓名",e1.sal "工资",deptno "部门编号"
from emp e1
where sal>(
select avg(sal)
from emp e2
group by deptno
having e1.deptno= e2.deptno
);

Insert picture description here

-6. The department name of the department with the highest average salary

select dname "部门名称"
from dept d
-- 根据id 关联两个表
where d.deptno=(
-- 求出的是最高工资的部门编号
select deptno 
from emp
group by deptno
having avg(sal)=
(select max(avg(sal))
from emp
group by deptno)
)

Insert picture description here

-7. The department name of the department with the lowest rank for finding the average salary

-- 1.每个部门的平均工资 
select avg(sal),deptno 
from emp 
group by deptno
-- 2.每个部门的平均工资的等级
select t.*,s.grade
from salgrade s ,(
select avg(sal) asal
from emp 
group by deptno) t
where t.asal between s.losal and s.hisal;

-- 3.最低部门的编号 和 工资等级
select deptno
from(select t.*,s.grade
from salgrade s ,(
select deptno, avg(sal) asal
from emp 
group by deptno) t
where t.asal between s.losal and s.hisal
order by grade asc)
where rownum=1

-- 4.根据部门编号(Id)进行表的关联
select dname
from dept d
where d.deptno=(
select deptno
from(select t.*,s.grade
from salgrade s ,(
select deptno, avg(sal) asal
from emp 
group by deptno) t
where t.asal between s.losal and s.hisal
order by grade asc)
where rownum=1);

Insert picture description here

– 8 Get the name of the manager whose salary is higher than the highest salary of ordinary employees (the employee code does not appear on the mgr field)

select e.ename, e.sal 
from emp e 
where e.empno in (Select distinct mgr from emp where mgr is not null) 
and
e.sal > (
select max(sal) maxSal 
from emp
where empno not in (Select distinct mgr from emp where mgr is not null))

Insert picture description here

–9 Get the top five employees with the highest salary

Insert picture description here

select *
  from(
  select *
  from emp
  order by sal desc
)
  where rownum<=5;

or

select *
from(
select rownum r , e.*
from (
select ename,sal 
from emp
order by sal desc
)e
where rownum<=5
)
where r>0;

Insert picture description here

–10 The sixth to tenth employees with the highest salary

 select *
  from(
  select rownum  ,t.*
  from (
    select  sal "工资",ename "员工"  
    from emp
    order by sal desc
    ) t
    where rownum<=10
)
  where rownum<=6;

Insert picture description here

Guess you like

Origin blog.csdn.net/agood_man/article/details/108685317