最近参与面试,作为刚入门的一名后端程序员,永远离不开增删改查,熟练使用mybatis。数据库成为了必考的一个分支,想想还是做点题来巩固一下以前的知识;
首先是三个表的的创建工作:
dept | CREATE TABLE `dept` (
`deptnu` int(11) NOT NULL COMMENT '部门编号',
`dname` varchar(50) DEFAULT NULL COMMENT '部门名称',
`addr` varchar(50) DEFAULT NULL COMMENT '部门地址',
PRIMARY KEY (`deptnu`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
employee | CREATE TABLE `employee` (
`empno` int(11) NOT NULL COMMENT '雇员编号',
`ename` varchar(50) DEFAULT NULL COMMENT '雇员姓名',
`job` varchar(50) DEFAULT NULL COMMENT '雇员职位',
`mgr` int(11) DEFAULT NULL COMMENT '雇员上级编号',
`hiredate` date DEFAULT NULL COMMENT '雇佣日期',
`sal` decimal(7,2) DEFAULT NULL COMMENT '薪资',
`deptnu` int(11) DEFAULT NULL COMMENT '部门编号',
PRIMARY KEY (`empno`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
salgrade | CREATE TABLE `salgrade` (
`grade` int(11) NOT NULL COMMENT '等级',
`lowsal` int(11) DEFAULT NULL COMMENT '最低薪资',
`higsal` int(11) DEFAULT NULL COMMENT '最高薪资',
PRIMARY KEY (`grade`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
然后插入数据:
/*插入dept表数据*/
INSERT INTO dept VALUES (10, '研发部', '北京');
INSERT INTO dept VALUES (20, '工程部', '上海');
INSERT INTO dept VALUES (30, '销售部', '广州');
INSERT INTO dept VALUES (40, '财务部', '深圳');
/*插入emp表数据*/
INSERT INTO employee VALUES (1009, '唐僧', '董事长', NULL, '2010-11-17', 50000, 10);
INSERT INTO employee VALUES (1004, '猪八戒', '经理', 1009, '2001-04-02', 29750, 20);
INSERT INTO employee VALUES (1006, '猴子', '经理', 1009, '2011-05-01', 28500, 30);
INSERT INTO employee VALUES (1007, '张飞', '经理', 1009, '2011-09-01', 24500,10);
INSERT INTO employee VALUES (1008, '诸葛亮', '分析师', 1004, '2017-04-19', 30000, 20);
INSERT INTO employee VALUES (1013, '林俊杰', '分析师', 1004, '2011-12-03', 30000, 20);
INSERT INTO employee VALUES (1002, '牛魔王', '销售员', 1006, '2018-02-20', 16000, 30);
INSERT INTO employee VALUES (1003, '程咬金', '销售员', 1006, '2017-02-22', 12500, 30);
INSERT INTO employee VALUES (1005, '后裔', '销售员', 1006, '2011-09-28', 12500, 30);
INSERT INTO employee VALUES (1010, '韩信', '销售员', 1006, '2018-09-08', 15000,30);
INSERT INTO employee VALUES (1012, '安琪拉', '文员', 1006, '2011-12-03', 9500, 30);
INSERT INTO employee VALUES (1014, '甄姬', '文员', 1007, '2019-01-23', 7500, 10);
INSERT INTO employee VALUES (1011, '妲己', '文员', 1008, '2018-05-23', 11000, 20);
INSERT INTO employee VALUES (1001, '小乔', '文员', 1013, '2018-12-17', 8000, 20);
/*插入salgrade表数据*/
INSERT INTO salgrade VALUES (1, 7000, 12000);
INSERT INTO salgrade VALUES (2, 12010, 14000);
INSERT INTO salgrade VALUES (3, 14010, 20000);
INSERT INTO salgrade VALUES (4, 20010, 30000);
INSERT INTO salgrade VALUES (5, 30010, 99990);
接着开始实战演练(练习的题基本是常见的面试题)
1.查出至少有一个员工的部门。显示部门编号、部门名称、部门位置、部门人数。
涉及表: employee dept
语句:select deptnu,count(*) from employee group by deptnu
语句:select a.deptnu,a.dname,a.addr, b.zongshu from dept a,(select deptnu,count(*) as
zongshu from employee group by deptnu) b where a.deptnu=b.deptnu;
2.列出薪金比安琪拉高的所有员工。
涉及表:employee
语句:select * from employee where sal > (select sal from employee where ename='安琪
拉');
3.列出所有员工的姓名及其直接上级的姓名。
涉及表:employee
语句:select a.ename,ifnull(b.ename,'BOSS') as leader from employee a left join employee
b on a.mgr=b.empno;
4.列出受雇日期早于直接上级的所有员工的编号、姓名、部门名称。
涉及表:employee dept
条件:a.hiredate < b.hiredate
语句:select a.empno,a.ename,c.dname from employee a left join employee b on
a.mgr=b.empno left join dept c on a.deptnu=c.deptnu where a.hiredate < b.hiredate;
5.列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门。
涉及表:dept employee
语句:select a.dname,b.* from dept a left join employee b on a.deptnu=b.deptnu;
6.列出所有文员的姓名及其部门名称,所在部门的总人数。
涉及表:employee dept
条件:job='文员'
语句:select deptnu,count(*) as zongshu from employee group by deptnu;
语句:select b.ename,a.dname,b.job,c.zongshu from dept a ,employee b ,(select
deptnu,count(*) as zongshu from employee group by deptnu) c where a.deptnu=b.deptnu and
b.job='文员' and b.deptnu=c.deptnu;
7.列出最低薪金大于15000的各种工作及从事此工作的员工人数。
涉及表:employee
条件:min(sal) > 15000
语句:select job,count(*) from employee group by job having min(sal) > 15000;
8.列出在销售部工作的员工的姓名,假定不知道销售部的部门编号。
涉及表:employee dept
select ename from employee where deptnu=(select deptnu from dept where dname='销售部');
9.列出与诸葛亮从事相同工作的所有员工及部门名称。
涉及表:employee dept
语句:select a.ename,b.dname from employee a,dept b where a.deptnu=b.deptnu and a.job=
(select job from employee where ename='诸葛亮');
语句:select a.ename,b.dname from employee a left join dept b on a.deptnu=b.deptnu where
a.job=(select job from employee where ename='诸葛亮');
10.列出薪金比 在部门30工作的员工的薪金 还高的员工姓名和薪金、部门名称。
涉及表:employee dept
语句:select a.ename,a.sal,b.dname from employee a ,dept b where a.deptnu=b.deptnu and
sal > (select max(sal) from employee where deptnu=30);
11.列出每个部门的员工数量、平均工资。
涉及表:employee
语句:select deptnu , count(*) ,avg (sal) from employee group by deptnu;
12.列出薪金高于公司平均薪金的所有员工信息,所在部门名称,上级领导,工资等级。
涉及表:employee dept salgrade
条件:select avg(sal) from employee
语句:elect a.*,c.dname,b.ename,d.grade from employee a,employee b,dept c ,salgrade d
where a.mgr=b.empno and a.deptnu =c.deptnu and a.sal > (select avg(sal) from
employee) and a.sal between d.lowsal and d.higsal;
练习到此为止,后端初中级别程序员,能够熟练完成以上习题,基本上面试阶段就够用了。但是长期的发展还是要继续努力,加油吧。