一、employees 表如下:
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`));
1. 查找最晚入职员工的所有信息
// 最晚入职的当天未必就一个人,也许有多人,使用排序并限制得只能取得指定数量的结果
select * from employees
where hire_date =
(select max(hire_date) from employees ) ;
2. 查找入职员工时间排名倒数第三的员工所有信息
select * from employees
where hire_date =
(select hire_date from employees order by hire_date desc limit 2,1);
二、dept_manager 和salaries 表如下:
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`));
1. 查找各个部门当前(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;
--------------------------
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`));
1. 查找所有已经分配部门的员工的last_name和first_name
select e.last_name, e.first_name,d.dept_no
from dept_emp d inner join employees e
on d.emp_no = e.emp_no ;
2. 查找所有员工的last_name和first_name以及对应部门编号dept_no,也包括展示没有分配具体部门的员工
SELECT ep.last_name, ep.first_name, dp.dept_no
FROM employees ep LEFT JOIN dept_emp dp
ON ep.emp_no = dp.emp_no;
四、employees 和 salaries 表如下:
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`));
1. 查找所有员工入职时候的薪水情况,给出emp_no以及salary, 并按照emp_no进行逆序
// 利用第一个表的入职时间找到工资表的入职工资,最后进行工号排序
WHERE e.emp_no = s.emp_no AND e.hire_date = s.from_date
ORDER BY e.emp_no DESC;
-------------------------------------------
SELECT e.emp_no, s.salary FROM employees AS e INNER JOIN salaries AS s
ON e.emp_no = s.emp_no AND e.hire_date = s.from_date
ORDER BY e.emp_no DESC
3. 找出所有员工当前(to_date='9999-01-01')具体的薪水salary情况,对于相同的薪水只显示一次,并按照逆序显示
// 在不同记录数较小时,count group by性能普遍高于count distinct,尤其对于text类型表现的更明显。而对于不同记录数较大的场景,count group by性能反而低于直接count distinct
SELECT DISTINCT salary FROM salaries WHERE to_date = '9999-01-01' ORDER BY salary DESC
------------------
select salary from salaries where to_date='9999-01-01' group by salary order by salary desc;
五、dept_manager,employees和salaries 表如下:
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`));
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`));
1. 获取所有部门当前manager的当前薪水情况,给出dept_no, emp_no以及salary,当前表示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;
SELECT emp_no FROM employees
WHERE emp_no NOT IN (SELECT emp_no FROM dept_manager)
------------------------------------------
1
2
|
select e.emp_no from employees as e left join dept_manager as d on e.emp_no = d.emp_no
where d.dept_no is null
|