SQL练习题23:获取员工其当前的薪水比其manager当前薪水还高的相关信息,当前表示to_date='9999-01-01', 结果第一列给出员工的emp_no, 第二列给出其manager的manager_no, 第三列给出该员工当前的薪水emp_salary, 第四列给该员工对应的manager当前的薪水manager_salary

题目:

获取员工其当前的薪水比其manager当前薪水还高的相关信息,当前表示to_date=‘9999-01-01‘,
结果第一列给出员工的emp_no,
第二列给出其manager的manager_no,
第三列给出该员工当前的薪水emp_salary,
第四列给该员工对应的manager当前的薪水manager_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 `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 a.emp_no,b.manager_no,a.emp_salary,b.manager_salary
FROM
(SELECT c1.emp_no, c1.dept_no, s1.salary AS emp_salary
FROM dept_emp AS c1, salaries AS s1
WHERE c1.emp_no = s1.emp_no
and s1.to_date=‘9999-01-01‘
and c1.to_date=‘9999-01-01‘
)AS a
INNER JOIN(
SELECT c2.emp_no AS manager_no, c2.dept_no, s2.salary AS manager_salary
FROM dept_manager AS c2,s alaries AS s2
WHERE c2.emp_no = s2.emp_no
and s2.to_date = ‘9999-01-01‘
and c2.to_date = ‘9999-01-01‘
)AS b
ON a.dept_no = b.dept_no
WHERE b.manager_salary < a.emp_salary

 

SQL练习题23:获取员工其当前的薪水比其manager当前薪水还高的相关信息,当前表示to_date='9999-01-01', 结果第一列给出员工的emp_no, 第二列给出其manager的manager_no, 第三列给出该员工当前的薪水emp_salary, 第四列给该员工对应的manager当前的薪水manager_salary

上一篇:oracle dump 及 events


下一篇:3.21 数据库优化