SQL_练习:获取员工其当前的薪水比其manager当前薪水还高的相关信息,获取员工其当前的薪水比其manager当前薪水还高的相关信息

结果第一列给出员工的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 de.emp_no emp_no,dm.emp_no manager_no,de.salary emp_salary,dm.salary manager_salary from
(dept_emp e inner join salaries s on e.emp_no=s.emp_no and s.to_date='9999-01-01') de,
(dept_manager m inner join salaries s on m.emp_no=s.emp_no and s.to_date='9999-01-01') dm
where de.dept_no=dm.dept_no and de.salary > dm.salary

思路:
1、inner join 表1、表3作为de(employees)
2、inner join 表2、表3作为dm(manager)
3、比较dept_no相同下的salary大小

猜你喜欢

转载自blog.csdn.net/weixin_42836351/article/details/81294790