Orcale database exercises men will be silent when they watch them, and women will cry when they watch them (middle)

–1. The 5 employees who got the last job

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

Insert picture description here

–2 Get how many employees each salary grade has

select s.grade "工资等级",count(*) "人数"
from  emp e,salgrade s
where sal between s.losal and s.hisal
group by  s.grade

Insert picture description here

-3 List the names of all employees and their immediate superiors

 -- nvl(null,0)  如果为null 返回0
select e1.ename,nvl(e2.ename,'没有上级')
from emp e1,emp e2
where e1.mgr=e2.empno(+)

Insert picture description here

-4 List the numbers, names, and department names of all employees whose employment date is earlier than their immediate superiors

select e.empno, e.ename, d.dname from emp e,emp m ,dept d
where e.hiredate < m.hiredate
and 
e.mgr = m.empno 
and
e.deptno = d.deptno

Insert picture description here

--5 List the department name and the employee information of these departments, and also list those departments without employees.

Use the right link because the department name to be displayed (and those departments without employees are listed at the same time) are all the departments in reality

select d.dname"部门名称" , e.*
from emp e,dept d
where e.deptno(+)=d.deptno

Insert picture description here

–6 List all departments with at least one employee

select deptno ,count(ename)
from emp
group by deptno;

Insert picture description here

–7 List all employees whose salary is more than "SMITH".

select *
from emp e
where e.sal>
(
select sal
from emp 
where ename='SMITH'
)

Insert picture description here

--8 --List all kinds of jobs where the minimum salary is greater than 1500 and the total number of employees engaged in this job

select j.job,c.co
from
(select job 
from emp 
group by job
having min(sal)>1500)j
,
(select job, count(*) co
from emp
group by job) c
where j.job=c.job

Insert picture description here

Guess you like

Origin blog.csdn.net/agood_man/article/details/108695465
Recommended