1、
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 * from employees
order byhire_date desc limit 0,1;
或者
SELECT * FROM employees WHERE hire_date = (SELECTMAX(hire_date) FROM employees);
LIMITm,n :表示从第m+1条开始,取n条数据;
LIMIT n: 表示从第0条开始,取n条数据,是limit(0,n)的缩写。
本题limit 2,1表示从第(2+1)条数据开始,取一条数据,即入职员工时间排名倒数第三的员工。
2、
同一
查找入职员工时间排名倒数第三的员工所有信息
SELECT * FROM employees
ORDER BYhire_date DESC LIMIT 2,1;
或
SELECT *
FROM employees
WHERE hire_date = (SELECT DISTINCT hire_date
FROMemployees
ORDERBY hire_date DESC
LIMIT2,1);
3、
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`));
查找各个部门当前(to_date='9999-01-01')领导当前薪水详情以及其对应部门编号dept_no
select salaries.*,dept_manager.dept_no from salaries,dept_manager
where salaries.emp_no = dept_manager.emp_no
and salaries.to_date = "9999-01-01"
and dept_manager.to_date = "9999-01-01";
4、
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`));
查找所有已经分配部门的员工的last_name和first_name
select e.last_name,e.first_name, d.dept_no
from dept_emp d, employees e
where d.emp_no = e.emp_no
5、
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`));
查找所有员工的last_name和first_name以及对应部门编号dept_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;