题目描述
1.查找最晚入职员工的所有信息
create database practice;
use practice;
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`)
);
insert into employees values(10008,'1958-02-19','Saniya','Kalloufi','M','1994-09-15');
insert into employees values(10007,'1958-03-29','Linda','Brown','M','1992-09-10');
insert into employees values(10006,'1960-05-03','Lili','Brown','M','1994-10-10');
insert into employees values(10005,'1962-04-04','Nancy','Brown','M','1993-04-23');
insert into employees values(10004,'1966-08-18','Mike','Smith','F','1996-02-10');
select * from employees order by hire_date desc limit 1;
/* 使用limit与offset关键字。offset是跳过几行元素,
例如找第二位入职的员工:select * from employees order by hire_date limit 1 offset 1;
按入职时间排序后,跳过第一行。 */
select * from employees order by hire_date desc limit 1 offset 0;
/* 使用limit关键字 从第0条记录 向后读取一个,也就是第一条记录 。左开右闭!
例如找最后两位入职的员工:select * from employees order by hire_date desc limit 0,2;
*/
select * from employees order by hire_date desc limit 0,1;
/* 使用子查询,最后一天的时间有多个员工信息 */
select * from employees where hire_date=(select max(hire_date) from employees);
2.查找入职员工时间排名倒数第三的员工所有信息
答案
/* LIMIT m,n : 表示从第m+1条开始,取n条数据;LIMIT n :表示从第0条开始,取n条数据,是limit(0,n)的缩写。*/
select * from employees order by hire_date desc limit 2,1;
/*使用limit与offset关键字。offset是跳过几行元素,*/
select * from employees order by hire_date desc limit 1 offset 2;
/*需要加distinct去重。
假设 5-23(入职最晚日期)入职的有a,b,c 3人;
5-22(入职第二晚日期)入职的有d,e 2人;
5-21(入职倒数第三晚)入职的有f,g,h 3人;
5-21前入职的若干...
若 不加distinct去重,那么按照日期倒序,limit 2,1(从倒数第2行开始,取一条数据)的查询结果为 5-23
加了distinct去重,会按入职日期进行分组,多个相同入职日期会分为一组,这样limit 2,1的结果即为 5-21。*/
select * from employees
where hire_date = (
select distinct hire_date from employees order by hire_date desc limit 2,1
)
3.查找各个部门当前(to_date=‘9999-01-01’)领导当前薪水详情以及其对应部门编号dept_no
CREATE TABLE dept_manager
(
dept_no
char(4) NOT NULL,
emp_no
int(11) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,dept_no
));
CREATE TABLE salaries
(
emp_no
int(11) NOT NULL,
salary
int(11) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,from_date
));
输出描述:
答案:
select salaries.*,dept_manager.dept_no
from salaries (inner) join dept_manager
on dept_manager.emp_no = salaries.emp_no
and(where) dept_manager.to_date = '9999-01-01'
and salaries.to_date = '9999-01-01';
select s.*,d.dept_no
from salaries as s
(inner)join dept_manager as d
on s.emp_no=d.emp_no
where(and) s.to_date='9999-01-01'
and d.to_date='9999-01-01';
select s.*,d.dept_no
from salaries s,dept_manager d
where s.to_date='9999-01-01'
and d.to_date='9999-01-01'
and s.emp_no=d.emp_no;
注:若答案为
select salaries.*,dept_manager.dept_no
from salaries
left join dept_manager
on salaries.emp_no=dept_manager.emp_no
and dept_manager.to_date=‘9999-01-01’
and salaries.to_date=‘9999-01-01’;
则结果如下图所示:
4.查找所有已经分配部门的员工的last_name和first_name以及dept_no
CREATE TABLE dept_emp
(
emp_no
int(11) NOT NULL,
dept_no
char(4) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,dept_no
));
CREATE TABLE employees
(
emp_no
int(11) NOT NULL,
birth_date
date NOT NULL,
first_name
varchar(14) NOT NULL,
last_name
varchar(16) NOT NULL,
gender
char(1) NOT NULL,
hire_date
date NOT NULL,
PRIMARY KEY (emp_no
));
答案
select
employees.last_name,
first_name,
dept_emp.dept_no
from
employees
inner join
dept_emp
on
employees.emp_no=dept_emp.emp_no;
select
e.last_name,
e.first_name,
d.dept_no
from
dept_emp as d
inner join
employees as e
on
e.emp_no = d.emp_no;
5.查找所有员工的last_name和first_name以及对应部门编号dept_no,也包括展示没有分配具体部门的员工
CREATE TABLE dept_emp
(
emp_no
int(11) NOT NULL,
dept_no
char(4) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,dept_no
));
CREATE TABLE employees
(
emp_no
int(11) NOT NULL,
birth_date
date NOT NULL,
first_name
varchar(14) NOT NULL,
last_name
varchar(16) NOT NULL,
gender
char(1) NOT NULL,
hire_date
date NOT NULL,
PRIMARY KEY (emp_no
));
答案:
select
e.last_name,
e.first_name,
d.dept_no
from
employees e
left join
dept_emp d
on
e.emp_no=d.emp_no
6.查找所有员工入职时候的薪水情况,给出emp_no以及salary, 并按照emp_no进行逆序
CREATE TABLE employees
(
emp_no
int(11) NOT NULL,
birth_date
date NOT NULL,
first_name
varchar(14) NOT NULL,
last_name
varchar(16) NOT NULL,
gender
char(1) NOT NULL,
hire_date
date NOT NULL,
PRIMARY KEY (emp_no
));
CREATE TABLE salaries
(
emp_no
int(11) NOT NULL,
salary
int(11) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,from_date
));
答案:
select
e.emp_no,
s.salary
from
employees as e
inner join
salaries as s
on
e.emp_no = s.emp_no
and
s.from_date=e.hire_date
order by
e.emp_no desc;
select
e.emp_no,
s.salary
from
employees as e,
salaries as s
where
e.emp_no=s.emp_no
and
e.hire_date=s.from_date
order by
e.emp_no desc;
7.查找薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数t
CREATE TABLE salaries
(
emp_no
int(11) NOT NULL,
salary
int(11) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,from_date
));
输出描述:
答案:
select emp_no,count(emp_no) as t
from salaries
group by emp_no having t>15
8.找出所有员工当前(to_date=‘9999-01-01’)具体的薪水salary情况,对于相同的薪水只显示一次,并按照逆序显示
CREATE TABLE salaries
(
emp_no
int(11) NOT NULL,
salary
int(11) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,from_date
));
select distinct
salary
from
salaries
where
to_date='9999-01-01'
order by
salary desc;
/*大表一般用distinct效率不高,大数据量的时候都
禁止用distinct,建议用group by解决重复问题。*/
select
salary
from
salaries
where
to_date='9999-01-01'
group by
salary
order by
salary desc;
9.获取所有部门当前manager的当前薪水情况,给出dept_no, emp_no以及salary,当前表示to_date='9999-01-01’
CREATE TABLE dept_manager
(
dept_no
char(4) NOT NULL,
emp_no
int(11) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,dept_no
));
CREATE TABLE salaries
(
emp_no
int(11) NOT NULL,
salary
int(11) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,from_date
));
select
d.dept_no,d.emp_no,s.salary
from
dept_manager as d
join
salaries as s
on
d.emp_no=s.emp_no
and
d.to_date = '9999-01-01'
and
s.to_date = '9999-01-01';
select
dept_manager.dept_no,
dept_manager.emp_no,
salaries.salary
from
salaries,dept_manager
where
dept_manager.to_date = '9999-01-01'
and
salaries.to_date = '9999-01-01'
and
dept_manager.emp_no = salaries.emp_no;
10.获取所有非manager的员工emp_no
CREATE TABLE dept_manager
(
dept_no
char(4) NOT NULL,
emp_no
int(11) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,dept_no
));
CREATE TABLE employees
(
emp_no
int(11) NOT NULL,
birth_date
date NOT NULL,
first_name
varchar(14) NOT NULL,
last_name
varchar(16) NOT NULL,
gender
char(1) NOT NULL,
hire_date
date NOT NULL,
PRIMARY KEY (emp_no
));
输出描述:
/*使用NOT IN选出在employees但不在dept_manager中的emp_no记录*/
select
emp_no
from
employees
where
emp_no
not in
(select emp_no from dept_manager)
/*先使用LEFT JOIN连接两张表,再从此表中选出dept_no值为NULL对应的emp_no记录*/
select
e.emp_no
from
employees e
left join
dept_manager d
on
e.emp_no = d.emp_no
where
d.emp_no is null;