获取员工其当前的薪水比其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));

思路:
#1.先构建两个表:员工工资表和经理工资表
#2.连接2张表,并且同时要求:salaries.emp_no = ‘9999-01-01’ ,员工工资>经理工资**

  1. select emp_salary.emp_no, manager_no, emp_salary, manager_salary
  2. from (
  3. select de.emp_no,de.dept_no,salary as emp_salary
  4. from dept_emp de join salaries s on de.emp_no = s.emp_no
  5. where s.to_date='9999-01-01'
  6. ) as emp_salary
  7. join (
  8. select dm.emp_no as manager_no,dm.dept_no, salary as manager_salary
  9. from dept_manager dm join salaries s on dm.emp_no = s.emp_no
  10. where s.to_date='9999-01-01'
  11. ) as manager_salary
  12. on emp_salary.dept_no=manager_salary.dept_no #连接条件为同部门
  13. where emp_salary > manager_salary