数据库常用语句面试题型

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";

  解析:连表即可


  

你可能感兴趣的:(相关面试的java试题)