少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

create table dept
(
deptno varchar(10) primary key,
dname varchar(10)
);
create table emp
(
empno varchar(10) primary key,
ename varchar(10),
job varchar(10),
mgr varchar(10),
sal varchar(10),
deptno varchar(10) references dept(deptno)
);
drop table dept;
drop table emp;
insert into dept values ('1','事业部');
insert into dept values ('2','销售部');
insert into dept values ('3','技术部');

insert into emp values ('01','jacky','clerk','tom','1000','1');
insert into emp values ('02','tom','clerk','','2000','1');
insert into emp values ('07','biddy','clerk','','2000','1');
insert into emp values ('03','jenny','sales','pretty','600','2');
insert into emp values ('04','pretty','sales','','800','2');
insert into emp values ('05','buddy','jishu','canndy','1000','3');
insert into emp values ('06','canndy','jishu','','1500','3');
select * from dept;
select * from emp;

 

1.列出emp表中各部门的部门号,最高工资,最低工资

  select e.deptno ,max(sal) ,min(sal) from emp as e group  by e.deptno;

  解析:各部门 提示:分组查询,所以用group by

 

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

   解析:各部门 提示:分组查询, job提示:条件查询,所以用到 group by 和 where

 

3 对于emp中最低工资小于2000的部门,列出job为'CLERK'的员工的部门号,最低工资,最高工资

   select b.deptno ,max(sal) ,min(sal) from emp as b where b.job="CLERK" 
   and (select min(sal) from emp as a where a.deptno=b.deptno)<2000 group  by b.deptno;

   解析:以上面两个题为基础,“emp中最低工资小于2000的部门”只是一个条件而已。

 

4 据部门号由高而低,工资有低而高列出每个员工的姓名,部门号,工资

   select * from emp as b order by b.deptno desc ,b.sal asc;

   解析:“部门号由高而低,工资有低”提示:需要排序,所以用到 order by 和 desc asc

 

5 列出'buddy'所在部门中每个员工的姓名与部门号

   select * from emp as b where b.deptno=(select a.deptno from emp as a where a.ename="buddy");

   解析:“'buddy'所在部门”提示找出他所在的部门号,所以可以理解“找出所有和buddy在同一部门的所有人”

 

6 列出每个员工的姓名,工作,部门号,部门名

   select ename,job,dept.deptno,dept.dname from emp,dept where emp.deptno=dept.deptno;

 

7 列出emp中工作为'CLERK'的员工的姓名,工作,部门号,部门名

   select ename,job,dept.deptno,dept.dname from emp,dept where emp.deptno=dept.deptno where job="CLERK";

 

8 对于emp中有管理者的员工,列出姓名,管理者姓名(管理者外键为mgr)

  select a.deptno,a.ename ,b.ename from emp as a,emp as b where a.mgr is not null and a.mgr=b.ename;

  解析:本句可以理解“一个员工他也是领导,所以emp.ename,emp.mgr 都有值并且有相等的员工”

 

9 对于dept表中,列出所有部门名,部门号,同时列出各部门工作为'CLERK'的员工名与工作

  select a.deptno ,a.dname, b.ename ,b.job from dept as a,emp as b where a.deptno=b.deptno and b.job="clerk";

  解析:连表即可



http://blog.csdn.net/shiyuntian_wang/article/details/4636799





建表:

DEPARTMENTS: DEPARTMENT_ID(primary key), DEPARTMENT_NAME, LOCATION
EMPLOYEES: EMPLOYEE_ID(primary key), EMPLOYEE_NAME, EMPLOYEE_JOB, MANAGER, SALARY, DEPARTMENT_ID
  1. 列出EMPLOYEES表中各部门的部门号,最高工资,最低工资
  2. select max(SALARY) as 最高工资,min(SALARY) as 最低工资,DEPARTMENT_ID from EMPLOYEES group by DEPARTMENT_ID;
  3. 列出EMPLOYEES表中各部门EMPLOYEE_JOB为'CLERK'的员工的最低工资,最高工资
  4. select max(SALARY) as 最高工资,min(SALARY) as 最低工资,DEPARTMENT_ID as 部门号 from EMPLOYEES where EMPLOYEE_JOB = 'CLERK' group by DEPARTMENT_ID;
  5. 对于EMPLOYEES中最低工资小于1000的部门,列出EMPLOYEE_JOB为'CLERK'的员工的部门号,最低工资,最高工资
  6. select max(SALARY) as 最高工资,min(SALARY) as 最低工资,DEPARTMENT_ID as 部门号 from EMPLOYEES as b where EMPLOYEE_JOB ='CLERK' and 1000>(select min(SALARY) from EMPLOYEES as a where a.DEPARTMENT_ID = b.DEPARTMENT_ID) group by b.DEPARTMENT_ID
  7. 根据部门号由高而低,工资有低而高列出每个员工的姓名,部门号,工资
  8. select DEPARTMENT_ID as 部门号,EMPLOYEE_NAME as 姓名,SALARY as 工资 from EMPLOYEES order by DEPARTMENT_ID desc,SALARY asc
  9. 列出'张三'所在部门中每个员工的姓名与部门号
  10. select EMPLOYEE_NAME,DEPARTMENT_ID from EMPLOYEES where DEPARTMENT_ID = (select DEPARTMENT_ID from EMPLOYEES where EMPLOYEE_NAME = '张三')
  11. 列出每个员工的姓名,工作,部门号,部门名
  12. select EMPLOYEE_NAME,EMPLOYEE_JOB,EMPLOYEES.DEPARTMENT_ID,DEPARTMENTS.DEPARTMENT_NAME from EMPLOYEES,DEPARTMENTS where EMPLOYEES.DEPARTMENT_ID = DEPARTMENTS.DEPARTMENT_ID
  13. 列出EMPLOYEES中工作为'CLERK'的员工的姓名,工作,部门号,部门名
  14. select EMPLOYEE_NAME,EMPLOYEE_JOB,DEPARTMENTS.DEPARTMENT_ID,DEPARTMENT_NAME from EMPLOYEES,DEPARTMENTS where DEPARTMENTS.DEPARTMENT_ID = EMPLOYEES.DEPARTMENT_ID and DEPARTMENT_JOB = 'CLERK'
  15. 对于EMPLOYEES中有管理者的员工,列出姓名,管理者姓名(管理者外键为MANAGER)
  16. select a.EMPLOYEE_NAME as 姓名,b.EMPLOYEE_NAME as 管理者 from EMPLOYEES as a,EMPLOYEES as b where a.MANAGER is not null and a.MANAGER = b.EMPLOYEE_ID
  17. 对于DEPARTMENTS表中,列出所有部门名,部门号,同时列出各部门工作为'CLERK'的员工名与工作
  18. select DEPARTMENT_NAME as 部门名,DEPARTMENTS.DEPARTMENT_ID as 部门号,EMPLOYEE_NAME as 员工名,EMPLOYEE_JOB as 工作 from DEPARTMENTS,EMPLOYEES where DEPARTMENTS.DEPARTMENT_ID *= EMPLOYEES.DEPARTMENT_ID and EMPLOYEE_JOB = 'CLERK'
  19. 对于工资高于本部门平均水平的员工,列出部门号,姓名,工资,按部门号排序
  20. select a.DEPARTMENT_ID as 部门号,a.EMPLOYEE_NAME as 姓名,a.SALARY as 工资 from EMPLOYEES as a where a.SALARY>(select avg(SALARY) from EMPLOYEES as b where a.DEPARTMENT_ID = b.DEPARTMENT_ID) order by a.DEPARTMENT_ID
  21. 对于EMPLOYEES,列出各个部门中平均工资高于本部门平均水平的员工数和部门号,按部门号排序
  22. select count(a.SALARY) as 员工数,a.DEPARTMENT_ID as 部门号 from EMPLOYEES as a where a.SALARY>(select avg(SALARY) from EMPLOYEES as b where a.DEPARTMENT_ID = b.DEPARTMENT_ID) group by a.DEPARTMENT_ID order by a.DEPARTMENT_ID
  23. 对于EMPLOYEES中工资高于本部门平均水平,人数多与1人的,列出部门号,人数,按部门号排序
  24. select count(a.EMPLOYEE_ID) as 员工数,a.DEPARTMENT_ID as 部门号,avg(SALARY) as 平均工资 from EMPLOYEES as a where (select count(c.EMPLOYEE_ID) from EMPLOYEES as c where c.DEPARTMENT_ID = a.DEPARTMENT_ID and c.SALARY>(select avg(SALARY) from EMPLOYEES as b where c.DEPARTMENT_ID = b.DEPARTMENT_ID))>1 group by a.DEPARTMENT_ID order by a.DEPARTMENT_ID
  25. 对于EMPLOYEES中低于自己工资至少5人的员工,列出其部门号,姓名,工资,以及工资少于自己的人数
  26. select a.DEPARTMENT_ID,a.EMPLOYEE_NAME,a.SALARY,(select count(b.EMPLOYEE_NAME) from EMPLOYEES as b where b.SALARY < a.SALARY) as 人数 from EMPLOYEES as a where (select count(b.EMPLOYEE_NAME) from EMPLOYEES as b where b.SALARY<a.SALARY)>5

http://blog.sina.com.cn/s/blog_4979ec3e0100r574.html

posted on 2012-07-18 14:05 abin 阅读(3857) 评论(0)  编辑  收藏 所属分类: Database

只有注册用户登录后才能发表评论。


网站导航: