方法一:

思路:

1、先求出非 manager 的员工编号、部门、和薪水。

2、再求出 manager 的员工编号、部门、和薪水。

3、员工和经理间用部门编号 dept_no 关联起来,

代码:

select
    a.emp_no,
    manager_no,
    emp_salary,
    manager_salary
from
    (
        select
            dp.emp_no,
            dp.dept_no,
            s.salary emp_salary
        from
            dept_emp dp,
            salaries s
        where
            dp.emp_no = s.emp_no
            and dp.emp_no not in (
                select
                    emp_no
                from
                    dept_manager
            )
            and s.to_date = '9999-01-01'
    ) a,
    (
        select
            dp.emp_no manager_no,
            dept_no,
            s.salary manager_salary
        from
            dept_emp dp,
            salaries s
        where
            dp.emp_no = s.emp_no
            and dp.emp_no in (
                select
                    emp_no
                from
                    dept_manager
            )
            and s.to_date = '9999-01-01'
    ) b
where
    a.dept_no = b.dept_no
    and a.emp_salary > b.manager_salary

方法二:

思路:

多表联接

代码:

SELECT
    dp.emp_no AS emp_no,
    dr.emp_no AS manager_no,
    s.salary AS emp_salary,
    m.salary AS manager_salary
FROM
    dept_emp dp
    LEFT JOIN dept_manager dr ON dp.dept_no = dr.dept_no
    LEFT JOIN salaries s ON dp.emp_no = s.emp_no
    LEFT JOIN salaries m ON dr.emp_no = m.emp_no
WHERE
    dp.emp_no <> dr.emp_no 
    AND s.salary > m.salary;