日期:2014-05-17  浏览次数:21064 次

如何快速提高写sql的能力?
哪个有oracle示例数据库scott下户查询的题目(附带答案的),谢谢了。

------解决方案--------------------
其于scott.emp,dept的SQL题目

1 列出emp表中各部门的部门号,最高工资,最低工资
[color=#FFFFFF]select max(sal) as 最高工资,min(sal) as 最低工资,deptno from emp group by deptno;[/color]

2 列出emp表中各部门job为'CLERK'的员工的最低工资,最高工资
[color=#FFFFFF]select max(sal) as 最高工资,min(sal) as 最低工资,deptno as 部门号 from emp where job = 'CLERK' group by deptno;[/color]

3 对于emp中最低工资小于1000的部门,列出job为'CLERK'的员工的部门号,最低工资,最高工资
[color=#FFFFFF]select max(sal) as 最高工资,min(sal) as 最低工资,deptno as 部门号 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[/color]

4 根据部门号由高而低,工资有低而高列出每个员工的姓名,部门号,工资
[color=#FFFFFF]select deptno as 部门号,ename as 姓名,sal as 工资 from emp order by deptno desc,sal asc
[/color]
5 写出对上题的另一解决方法
[color=#FFFFFF](请补充)[/color]

6 列出'张三'所在部门中每个员工的姓名与部门号
[color=#FFFFFF]select ename,deptno from emp where deptno = (select deptno from emp where ename = '张三')[/color]

7 列出每个员工的姓名,工作,部门号,部门名
[color=#FFFFFF]select ename,job,emp.deptno,dept.dname from emp,dept where emp.deptno=dept.deptno[/color]

8 列出emp中工作为'CLERK'的员工的姓名,工作,部门号,部门名
[color=#FFFFFF]select ename,job,dept.deptno,dname from emp,dept where dept.deptno=emp.deptno and job='CLERK'[/color]

9 对于emp中有管理者的员工,列出姓名,管理者姓名(管理者外键为mgr)
[color=#FFFFFF]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[/color]

10 对于dept表中,列出所有部门名,部门号,同时列出各部门工作为'CLERK'的员工名与工作
[color=#FFFFFF]select dname as 部门名,dept.deptno as 部门号,ename as 员工名,job as 工作 from dept,emp
where dept.deptno *= emp.deptno and job = 'CLERK'[/color]

11 对于工资高于本部门平均水平的员工,列出部门号,姓名,工资,按部门号排序
[color=#FFFFFF]select a.deptno as 部门号,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[/color]

12 对于emp,列出各个部门中平均工资高于本部门平均水平的员工数和部门号,按部门号排序
[color=#FFFFFF]select count(a.sal) as 员工数,a.deptno as 部门号 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[/color]

13 对于emp中工资高于本部门平均水平,人数多与1人的,列出部门号,人数,按部门号排序
[color=#FFFFFF]select count(a.empno) as 员工数,a.deptno as 部门号,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[/color]

14 对于emp中低于自己工资至少5人的员工,列出其部门号,姓名,工资,以及工资少于自己的人数
[color=#FFFFFF]select a.deptno,a.ename,a.sal,(select count(b.ename) from emp as b where b.sal<a.sal) as 人数 from emp as a
where (select count(b.ename) from emp as b where b.sal<a.sal)>5[/color]
------解决方案--------------------
SQL code

connect scott/tiger;

-- 查询表结构
desc emp;
desc dept;
select * from tab;
select * from tab where tabtype='TABLE';

--1.列出emp表中各部门的部门号,最高工资,最低工资
 select deptno as 部门号, max(sal) as 最高工资,min(sal) as 最低工资 from emp group by deptno;
 
--2.列出emp表中各部门job为'CLERK'的员工的最低工资,最高工资
  select deptno as 部门号, max(sal) as 最高工资, min(sal) as 最低工资 from emp where job ='CLERK' group by deptno;

--3.对于emp中最低工资小于1000的部门,列出job为'CLERK'的员工的部门号,最低工资,最高工资
-- select max(sal) as 最高工资, min(sal) as 最低工资  from emp where job ='CLERK' group by deptno having min(sal)<1000;
select deptno as 部门号, max(sal) as 最高工资, min(sal) as 最低工资 from emp where job ='CLERK'
and deptno in(select deptno from emp group by deptno having min(sal)<1000) group by deptno;

--4.上题的另一解决方法
select deptno as 部门号, max(sal) as 最高工资, min(sal) as 最低工资 from emp a  where job ='CLERK'
and 1000>(select min(sal) as 最低工资 from emp b where a.deptno=b.deptno) group by deptno;

--5.根据部门号由高而低,工资由低而高列出每个员工的姓名,部门号,工资
select ename,deptno,sal fr