测试⼯程师⾯试题mysql_测试⼯程师sql笔试题都在这⾥了!创建表(以下操作基于mysql)
员⼯ employees
⼯资表 salary
部门 departments
create or replace table employees(
empID INT(10) NOT NULL UNIQUE PRIMARY KEY ,
empName VARCHAR(20) NOT NULL , #姓名
sex VARCHAR(4) , #性别
birth date, #出⽣⽇期
deptID INT(20) , #部门编码
jobs VARCHAR(20) , #⼯作岗位
firJob date, #开始⼯作的⽇期
hiredate date,#⼊职⽇期
politicalStatus VARCHAR(20) , # 政治⾯貌
leader INT(10) #领导编码
);
create or replace table salary(
sid INT(10) NOT NULL UNIQUE PRIMARY KEY ,
empID INT(10) not null,
salary INT(10) not null,#⼯资
lastedit date #上次调薪⽇期
);
create or replace table departments(
deptid INT(10) NOT NULL UNIQUE PRIMARY KEY ,
用kruskal算法求最小生成树deptname VARCHAR(20) not null,#部门名称
faterdeptid INT(10) # 上级部门编码
jdbc的中文意思
);
插⼊数据
SQL数据库查询练习
⼀、单表查询
1、显⽰所有职⼯的基本信息。
2、查询所有职⼯所属部门的部门号,不显⽰重复的部门号。
3、求出所有职⼯的⼈数。
4、列出最⾼⼯和最低⼯资。
5、列出职⼯的平均⼯资和总⼯资。
6、创建⼀个只有职⼯号、姓名和参加⼯作的新表,名为⼯作⽇期表。
数三声组词大全两个字
7、显⽰所有⼥职⼯的年龄
8、列出所有姓刘的职⼯的职⼯号、姓名和出⽣⽇期。
9、列出1990年以前出⽣的职⼯的姓名、参加⼯作⽇期。
10、列出总⼈数⼤于4的部门号和总⼈数。
11、列出所有陈姓和李姓的职⼯姓名。
12、列出所有部门号为1002和1003的职⼯号、姓名。
13、将职⼯表worker中的职⼯按出⽣的先后顺序排序。
14、求出各部门党员的⼈数。
⼆、多表查询
1、列出每名职⼯的职⼯号、姓名和部门名。
2、列出市场部的所有⼥职⼯的姓名和政治⾯貌。
3、显⽰所有职⼯的姓名、部门名和⼯资数。
4、显⽰所有职⼯的职⼯号、姓名、部门名和⼯资,并按部门名顺序排列。
5、显⽰各部门名和该部门的所有职⼯平均⼯资。
6、显⽰所有平均⼯资⾼于1200的部门名和对应的平均⼯资。
7、查询刘欣所在的部门。
8、查询部门名为财务处的职⼯的情况。
9、列出⼯资在1000-2000之间的所有职⼯姓名。
10、显⽰⼯资最⾼的前3名职⼯的职⼯号和姓名。
SQL数据库查询练习(参考答案)
⼀、单表查询
1、显⽰所有职⼯的基本信息。
select * from employees
2、查询所有职⼯所属部门的部门号,不显⽰重复的部门号。
select distinct e.deptID from employees e
3、求出所有职⼯的⼈数。
select count(1) from employees
4、列出最⾼⼯和最低⼯资。
select max(s.salary) as '最⾼⼯资',min(s.salary) as '最低⼯资' from salary s 5、列出职⼯的平均⼯资和总⼯资。
select AVG(s.salary) as '最⾼⼯资',SUM(s.salary) as '最低⼯资' from salary s
6、创建⼀个只有职⼯号、姓名和参加⼯作的新表,名为⼯作⽇期表。
Create table emp2 (pName,e.firJob from employees e);
在其他数据库中,可以尝试使⽤pName,e.firJob into newtable from employees e;
7、显⽰所有⼥职⼯的年龄
pID, e.empName, (DATEDIFF(CURDATE(),e.birth) DIV 365) as '年龄' from employees e where e.sex = '⼥';
8、列出所有姓刘的职⼯的职⼯号、姓名和出⽣⽇期。
pName,e.birth from employees e pName like '张%'
9、列出1990年以前出⽣的职⼯的姓名、参加⼯作⽇期。
pName,e.firJob from employees e where YEAR(e.firJob)<1990
10、列出总⼈数⼤于4的部门号和总⼈数。
select e.deptID, count(1) from employees e group by e.deptID having count(1)>4
11、列出所有陈姓和李姓的职⼯姓名。
pName from employees e pName like'张%' pName like'李%'
12、列出所有部门号为1002和1003的职⼯号、姓名。
多重阴影流程攻略
pID, e.empName from employees e where e.deptID in (1002,1003);
13、将职⼯表worker中的职⼯按出⽣的先后顺序排序。
select * from employees e order by e.birth asc
14、求出各部门党员的⼈数。
select e.deptID,count(1) from employees e where e.politicalStatus = '党员' GROUP BY e.deptID
⼆、多表查询
1、列出每名职⼯的职⼯号、姓名和部门名。
pID, e.empName, d.deptname from employees e,departments d where e.deptID = d.deptid
2、列出市场部的所有⼥职⼯的姓名和政治⾯貌。
pName,e.politicalStatus,d.deptname from employees e,departments d where e.deptID = d.deptid and e.sex= '⼥' and d.deptname = '市场部'
3、显⽰所有职⼯的姓名、部门名和⼯资数。
pName,d.deptname,s.salary from employees e LEFT JOIN departments d on e.deptID = d.deptid LEFT JOIN salary s pID = s.empID
4、显⽰所有职⼯的职⼯号、姓名、部门名和⼯资,并按部门名顺序排列。
pid, e.empName,d.deptname,s.salary from employees e LEFT JOIN departments d on e.deptID = d.deptid LEFT JOIN salary s pID = s.empID ORDER BY e.deptID
5、显⽰各部门名和该部门的所有职⼯平均⼯资。
SELECT d.deptname, AVG(s.salary) from departments d LEFT JOIN employees e on d.deptid = e.deptID LEFT JOIN salary s pID = s.empID GROUP BY d.deptname
6、显⽰所有平均⼯资⾼于1200的部门名和对应的平均⼯资。
SELECT d.deptname, AVG(s.salary) from departments d LEFT JOIN employees e on d.deptid = e.deptID LEFT JOIN salary s pID = s.empID GROUP BY d.deptname having AVG(s.salary)>1200
7、查询刘欣所在的部门。
select d.deptname from employees e, departments d where e.deptID = d.deptid pName = '刘欣'
java一个方法调用另一个方法或者
select d.deptname from departments d where d.deptid =(select deptid from employees e pName ='刘欣')
8、查询部门名为财务处的职⼯的情况。mysql面试题sql
select * from employees e,departments d where e.deptID = d.deptid and d.deptname ='财务处'
或者
select * from employees e where e.deptid =(select deptid from departments d where d.deptname ='财务处')
9、列出⼯资在1000-2000之间的所有职⼯姓名。
pName,s.salary from salary s,employees e where s.salary between 1000 and 2000 pID = e.empID
10、显⽰⼯资最⾼的前3名职⼯的职⼯号和姓名。
pID, e.empName from salary s, employees e pID = e.empID ORDER BY s.salary desc LIMIT 3