1、查找最晚入职员工的所有信息
1
2
3
4
5
6
7
8
|
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
2
3
4
5
6
|
SELECT *
FROM employees
WHERE hire_date = (
SELECT MAX(hire_date)
FROM employees
);
|
2、查找入职员工时间排名倒数第三的员工所有信息
1
2
3
4
5
6
7
8
|
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
2
3
4
5
6
7
8
|
SELECT * FROM employees
WHERE hire_date = (
SELECT hire_date
FROM employees
GROUP BY hire_date
ORDER BY hire_date DESC
LIMIT 2, 1
)
|
3、 查找各部门当前领导当前薪水详情以及其对应部门编号
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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
2
3
4
5
6
|
SELECT s.*, dept_no
FROM dept_manager m
JOIN salaries s
ON m.emp_no = s.emp_no
AND m.to_date = '9999-01-01'
AND s.to_date = '9999-01-01'
|
4、 查找所有已经分配部门的员工的last_name和first_name
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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
2
3
4
|
SELECT last_name, first_name, dept_no
FROM employees e
LEFT JOIN dept_emp d
ON e.emp_no = d.emp_no;
|
5、 查找所有员工的last_name和first_name以及对应部门编号dept_no
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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
2
3
4
|
SELECT last_name, first_name, dept_no
FROM employees e
LEFT JOIN dept_emp d
ON e.emp_no = d.emp_no;
|
6、 查找所有员工入职时候的薪水情况
给出emp_no以及salary, 并按照emp_no进行逆序。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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
2
3
4
5
|
SELECT e.emp_no, salary
FROM employees e
JOIN salaries s
ON e.emp_no = s.emp_no AND e.hire_date = s.from_date
ORDER BY e.emp_no DESC;
|
7、 查找薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数t
1
2
3
4
5
6
|
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
2
3
4
|
SELECT emp_no, COUNT(*) AS t
FROM salaries
GROUP BY emp_no
HAVING t > 15;
|
文章作者
版权所有,转载请注明出处。工作请联系qiuyuling98@163.com
上次更新
2019-10-16
许可协议
原创文章,如需转载请注明文章作者和出处。谢谢!