- MySQL中的TOP-N语句
- limit后start, count
select *
from emp
order by sal
limit 0, 5
- MySQL中分页公式
select *
from emp
limit (pageIndex-1)*pageSize, pageSize
练习1.1 查询入职日期最早的员工姓名,入职日期。
select ename, hiredate
from emp
where hiredate=
(select min(hiredate)
from emp )
1.2 查询工资比SMITH工资高并且工作地点在CHICAGO的员工姓名,工资,部门名称。
select e.ename, e.sal, d.dname
from emp e
join dept d on e.deptno=d.deptno
where e.sal>
(select sal
from emp
where ename='SMITH')
and d.loc='CHICAGO'
1.3 查询入职日期比20部门入职日期最早的员工还要早的员工姓名,入职日期。
select ename, hiredate
from emp
where hiredate<
(select min(hiredate)
from emp
where deptno=20)
1.4 查询部门人数大于所有部门平均人数的部门编号,部门名称,部门人数。
select e.deptno, d.dname, count(e.deptno)
from emp e
join dept d on e.deptno=d.deptno
group by e.deptno, d.dname
having count(e.deptno)>
(select avg(count(deptno))
from emp
group by deptno)
练习2.1 查询入职日期比10部门任意一个员工晚的员工姓名、入职日期,不包括10部门员工。
select ename, hiredate
from emp
where hiredate>any
(select hiredate
from emp
where deptno=10)
and deptno<>10
2.2 查询入职日期比10部门所有员工晚的员工姓名、入职日期,不包括10部门员工。
select ename, hiredate
from emp
where hiredate>all
(select hiredate
from emp
where deptno=10)
and deptno<>10
2.3 查询职位和10部门任意一个员工职位相同的员 工姓名,职位,不包括10部门员工。
select ename, job
from emp
where job=any
(select job
from emp
where deptno=10)
and deptno<>10
练习3.1 查询职位及经理和10部门任意一个员工职位及经理相同的员工姓名,职位,不包括10部门员工。
select ename, job
from emp
where (job,mgr)in
(select job, mgr
from emp
where deptno=10)
and deptno<>10
3.2 查询职位及经理和10部门任意一个员工职位或经理相同的员工姓名,职位,不包括10部门员工。
select ename, job
from emp
where job in
(select job
from emp
where deptno=10)
or mgr in
(select mgr
from emp
where deptno=10)
and deptno<>10
查询比自己部门平均工资高的员工姓名,工资,
部门编号,部门平均工资
select e.ename, e.sal, e.deptno, d.salavg
from emp e
join (select deptno, avg(sal) salavg
from emp
group by deptno ) d
on e.deptno=d.deptno
where d.salavg
练习4.1 查询比自己职位平均工资高的员工姓名、职位,部门名称,职位平均工资。
select e.ename, e.job, d.dname, b.job_avgsal
from emp e
join (select job, avg(sal) job_avgsal
from emp
group by job) b
on e.job=b.job
and e.sal>b.job_avgsal
join dept d on e.deptno=d.deptno
4.2 查询职位和经理同员工SCOTT或BLAKE完全相同的员工姓名、职位,不包括SCOTT和BLAKE本人。
select e.ename, e.job
from emp e
where (e.job, e.mgr) in
(select job, mgr
from emp
where ename in ('SCOTT', 'BLAKE'))
and e.ename not in ('SCOTT','BLAKE')
4.3 查询不是经理的员工姓名。
select ename
from emp
where empno not in
(select mgr
from emp
where mgr is not null)
练习5.1 查询入职日期最早的前5名员工姓名,入职日期。
select rownum, ename,hiredate
from (select ename,hiredate
from emp
order by hiredate asc)
where rownum<=5
5.2 查询工作在CHICAGO并且入职日期最早的前2名的员工姓名,入职日期。
select rownum, ename, hiredate
from (select deptno, ename, hiredate
from emp
order by hiredate asc) e
join dept d on e.deptno=d.deptno
and d.loc='CHICAGO'
where rownum<=2
练习6 按照每页显示5条记录,分别查询第1页,第2 页,第3页信息,要求显示员工姓名、入职日期 、部门名称。
select *
from (select rownum rn, e.ename, e.hiredate, d.dname
from emp e
join dept d on e.deptno=d.deptno
where rownum<=5)
where rn>0;
select *
from (select rownum rn, e.ename, e.hiredate, d.dname
from emp e
join dept d on e.deptno=d.deptno
where rownum<=10)
where rn>5;
select *
from (select rownum rn, e.ename, e.hiredate, d.dname
from emp e
join dept d on e.deptno=d.deptno
where rownum<=15)
where rn>10;
练习7 按照每页显示5条记录,分别查询工资最高的第1页,第2页,第3页信息,要求显示员工姓名 、入职日期、部门名称、工资。
select *
from (select rownum rn, b.*
from (select e.ename, e.hiredate, d.dname, e.sal
from emp e
join dept d on e.deptno=d.deptno
order by e.sal desc) b
where rownum<=5)
where rn>0;
select *
from (select rownum rn, b.*
from (select e.ename, e.hiredate, d.dname, e.sal
from emp e
join dept d on e.deptno=d.deptno
order by e.sal desc) b
where rownum<=10)
where rn>5;
select *
from (select rownum rn, b.*
from (select e.ename, e.hiredate, d.dname, e.sal
from emp e
join dept d on e.deptno=d.deptno
order by e.sal desc) b
where rownum<=15)
where rn>10;
课后作业
- 查询工资高于编号为7782的员工工资,并且和7369号员工从事相同工作的员工的编号、姓名及工资。
select empno, ename, job
from emp
where sal>(select sal
from emp
where empno=7782)
and job=(select job
from emp
where empno=7369)
- 查询工资最高的员工姓名和工资。
select ename, sal
from emp
where sal=(select max(sal)
from emp)
- 查询部门最低工资高于10号部门最低工资的部门的编号、名称及部门最低工资。
select e.deptno, d.dname, min(sal)
from emp e
join dept d on e.deptno=d.deptno
group by e.deptno, d.dname
having min(sal)>
(select min(sal)
from emp
where deptno=10)
- 查询员工工资为其部门最低工资的员工的编号和姓名及工资。
select e.empno, e.ename, e.sal
from emp e
join (select deptno, min(sal) min_sal
from emp
group by deptno) b
on e.deptno=b.deptno
where e.sal=b.min_sal
- 显示经理是KING的员工姓名,工资。
select e.ename, e.sal
from emp e
join (select empno
from emp
where ename='KING') m
on e.mgr=m.empno
- 显示比员工SMITH参加工作时间晚的员工姓名,工资,参加工作时间。
select ename, sal, hiredate
from emp
where hiredate>
(select hiredate
from emp
where ename='SMITH')
- 使用子查询的方式查询哪些职员在NEW YORK工作。
select ename
from emp
where ename in
(select e.ename
from emp e
join dept d on e.deptno=d.deptno
where d.loc='NEW YORK')
- 写一个查询显示和员工SMITH工作在同一个部门的员工姓名,雇佣日期,查询结果中排除SMITH。
select e.ename, e.hiredate
from emp e
join (select deptno
from emp
where ename='SMITH'
group by deptno) b
on e.deptno=b.deptno
where e.ename<>'SMITH'
- 写一个查询显示其工资比全体职员平均工资高的员工编号,姓名。
select empno, ename
from emp
where sal>
(select avg(sal)
from emp )
- 写一个查询显示其上级领导是KING的员工姓名、工资。
select e.ename, e.sal
from emp e
join (select *
from emp ) m
on e.mgr=m.empno
where m.ename='KING'
- 显示所有工作在RESEARCH部门的员工姓名,职位。
select e.ename, e.job
from emp e
join dept d on e.deptno=d.deptno
where d.dname='RESEARCH'
- 查询每个部门的部门编号、平均工资,要求部门的平均工资高于部门20的平均工资。
select deptno, avg(sal)
from emp
group by deptno
having avg(sal)>
(select avg(sal)
from emp
where deptno=20
group by deptno)
- 查询大于自己部门平均工资的员工姓名,工资,所在部门平均工资,高于部门平均工资的额度。
select e.ename, e.sal, b.avg_sal, e.sal-b.avg_sal oversal
from emp e
join (select deptno, avg(sal) avg_sal
from emp
group by deptno ) b
on e.deptno=b.deptno
where e.sal>b.avg_sal
- 列出至少有一个雇员的所有部门。
select deptno, count(*)
from emp
group by deptno
having count(*)>0
- 列出薪金比"SMITH"多的所有雇员。
select ename, sal
from emp
where sal>
(select sal
from emp
where ename='SMITH')
- 列出入职日期早于其直接上级的所有雇员。
select e.ename, e.hiredate
from emp e
join emp m
on e.mgr=m.empno
where e.hiredate
- 找员工姓名和直接上级的名字。
select e.ename, m.ename
from emp e
join emp m on e.mgr=m.empno
- 显示部门名称和人数。
select d.dname, count(*)
from emp e
join dept d on e.deptno=d.deptno
group by d.dname
- 显示每个部门的最高工资的员工。
select ename
from emp
where sal in
(select max(sal)
from emp
group by deptno)
- 显示出和员工号7369部门相同的员工姓名,工资。
select ename, sal
from emp
where deptno=
(select deptno
from emp
where empno=7369)
- 显示出和姓名中包含"W"的员工相同部门的员工姓名。
select ename
from emp
where deptno=
(select deptno
from emp
where ename like '%W%')
- 显示出工资大于平均工资的员工姓名,工资。
select ename, sal
from emp
where sal>
(select avg(sal)
from emp)
- 显示出工资大于本部门平均工资的员工姓名,工资。
select e.ename, e.sal
from emp e
join (select deptno, avg(sal) avg_sal
from emp
group by deptno) b
on e.deptno=b.deptno
where e.sal>b.avg_sal
- 显示每位经理管理员工的最低工资,及最低工资者的姓名。
select e.ename, m.min_sal
from emp e
join (select min(sal) min_sal
from emp
where mgr is not null
group by mgr) m
on m.min_sal=e.sal
- 显示比工资最高的员工参加工作时间晚的员工姓名,参加工作时间。
select ename, hiredate
from emp
where hiredate>
(select hiredate
from emp
where sal=
(select max(sal)
from emp ))
- 显示出平均工资最高的部门平均工资及部门名称。
select d.dname, avg(sal)
from emp e
join dept d on e.deptno=d.deptno
group by d.dname
having avg(sal)=
(select max(avg(sal))
from emp
group by deptno