/*
建表:
dept:
deptno(primary key),dname,loc
emp:
empno(primary key),ename,job,mgr,sal,deptno
*/
1 列出emp表中各部門的部門號(hào),最高工資,最低工資
select max(sal) as 最高工資,min(sal) as 最低工資,deptno from emp group by deptno;
2 列出emp表中各部門job為'CLERK'的員工的最低工資,最高工資
select max(sal) as 最高工資,min(sal) as 最低工資,deptno as 部門號(hào) from emp where job = 'CLERK' group by deptno;
3 對(duì)于emp中最低工資小于1000的部門,列出job為'CLERK'的員工的部門號(hào),最低工資,最高工資
select max(sal) as 最高工資,min(sal) as 最低工資,deptno as 部門號(hào) from emp as b
where job='CLERK' and 1000>(select min(sal) from emp as a where a.deptno=b.deptno) group by b.deptno
4 根據(jù)部門號(hào)由高而低,工資有低而高列出每個(gè)員工的姓名,部門號(hào),工資
select deptno as 部門號(hào),ename as 姓名,sal as 工資 from emp order by deptno desc,sal asc
5 寫出對(duì)上題的另一解決方法
(請(qǐng)補(bǔ)充)
6 列出'張三'所在部門中每個(gè)員工的姓名與部門號(hào)
select ename,deptno from emp where deptno = (select deptno from emp where ename = '張三')
7 列出每個(gè)員工的姓名,工作,部門號(hào),部門名
select ename,job,emp.deptno,dept.dname from emp,dept where emp.deptno=dept.deptno
8 列出emp中工作為'CLERK'的員工的姓名,工作,部門號(hào),部門名
select ename,job,dept.deptno,dname from emp,dept where dept.deptno=emp.deptno and job='CLERK'
9 對(duì)于emp中有管理者的員工,列出姓名,管理者姓名(管理者外鍵為mgr)
select a.ename as 姓名,b.ename as 管理者 from emp as a,emp as b where a.mgr is not null and a.mgr=b.empno
10 對(duì)于dept表中,列出所有部門名,部門號(hào),同時(shí)列出各部門工作為'CLERK'的員工名與工作
select dname as 部門名,dept.deptno as 部門號(hào),ename as 員工名,job as 工作 from dept,emp
where dept.deptno *= emp.deptno and job = 'CLERK'
11 對(duì)于工資高于本部門平均水平的員工,列出部門號(hào),姓名,工資,按部門號(hào)排序
select a.deptno as 部門號(hào),a.ename as 姓名,a.sal as 工資 from emp as a
where a.sal>(select avg(sal) from emp as b where a.deptno=b.deptno) order by a.deptno
12 對(duì)于emp,列出各個(gè)部門中平均工資高于本部門平均水平的員工數(shù)和部門號(hào),按部門號(hào)排序
select count(a.sal) as 員工數(shù),a.deptno as 部門號(hào) from emp as a
where a.sal>(select avg(sal) from emp as b where a.deptno=b.deptno) group by a.deptno order by a.deptno
13 對(duì)于emp中工資高于本部門平均水平,人數(shù)多與1人的,列出部門號(hào),人數(shù),按部門號(hào)排序
select count(a.empno) as 員工數(shù),a.deptno as 部門號(hào),avg(sal) as 平均工資 from emp as a
where (select count(c.empno) from emp as c where c.deptno=a.deptno and c.sal>(select avg(sal) from emp as b where c.deptno=b.deptno))>1
group by a.deptno order by a.deptno
14 對(duì)于emp中低于自己工資至少5人的員工,列出其部門號(hào),姓名,工資,以及工資少于自己的人數(shù)
select a.deptno,a.ename,a.sal,(select count(b.ename) from emp as b where b.sal<a.sal) as 人數(shù) from emp as a
where (select count(b.ename) from emp as b where b.sal<a.sal)>5
轉(zhuǎn)自:http://blog.csdn.net/woolceo/archive/2006/03/02/614094.aspx
posted on 2006-03-04 20:31
風(fēng)蕭蕭 閱讀(2041)
評(píng)論(1) 編輯 收藏 所屬分類:
雜談