SQL练习题13:获取所有部门中当前(dept_emp.to_date = '9999-01-01')员工当前(salaries.to_date='9999-01-01')薪水最高的相关信息,给出dept_no, emp_no以及其对应的salary,按照部门升序排列。

题目:

获取所有部门中当前(dept_emp.to_date = ‘9999-01-01‘)员工当前(salaries.to_date=‘9999-01-01‘)薪水最高的相关信息,给出dept_no, emp_no以及其对应的salary,按照部门升序排列。
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 `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`));
如插入:
INSERT INTO dept_emp VALUES(10001,‘d001‘,‘1986-06-26‘,‘9999-01-01‘);
INSERT INTO dept_emp VALUES(10002,‘d001‘,‘1996-08-03‘,‘9999-01-01‘);
INSERT INTO dept_emp VALUES(10003,‘d001‘,‘1996-08-03‘,‘1997-08-03‘);

INSERT INTO salaries VALUES(10001,90000,‘1986-06-26‘,‘1987-06-26‘);
INSERT INTO salaries VALUES(10001,88958,‘2002-06-22‘,‘9999-01-01‘);
INSERT INTO salaries VALUES(10002,72527,‘1996-08-03‘,‘1997-08-03‘);
INSERT INTO salaries VALUES(10002,72527,‘2000-08-02‘,‘2001-08-02‘);
INSERT INTO salaries VALUES(10002,72527,‘2001-08-02‘,‘9999-01-01‘);
INSERT INTO salaries VALUES(10003,90000,‘1996-08-03‘,‘1997-08-03‘);
则输出
dept_no
emp_no
salary
d001
10001
88958

程序:

SELECT a.dept_no, a.emp_no, MAX(b.salary)
FROM dept_emp AS a INNER JOIN salaries AS b
ON a.emp_no = b.emp_no
WHERE a.to_date = ‘9999-01-01‘
AND b.to_date = ‘9999-01-01‘
GROUP BY a.dept_no
ORDER BY a.dept_no ASC

 

SQL练习题13:获取所有部门中当前(dept_emp.to_date = '9999-01-01')员工当前(salaries.to_date='9999-01-01')薪水最高的相关信息,给出dept_no, emp_no以及其对应的salary,按照部门升序排列。

上一篇:MySQL中concat()、concat_ws()、group_concat()函数的使用技巧与心得总结


下一篇:tcpcollect基于libpcap 监听mysql sql