题解 | #获取员工其当前的薪水比其当前薪水还高的相关信息#
获取员工其当前的薪水比其manager当前薪水还高的相关信息
https://www.nowcoder.com/practice/f858d74a030e48da8e0f69e21be63bef
SELECT t1.emp_no, t1.manager_no, t1.emp_salary, s2.salary AS manager_salar FROM ( SELECT de.emp_no AS emp_no, de.dept_no AS dept_no, dm.emp_no AS manager_no, s1.salary AS emp_salary FROM dept_emp de JOIN dept_manager dm USING (dept_no) JOIN salaries s1 ON de.emp_no = s1.emp_no WHERE de.emp_no <> dm.emp_no ) AS t1 JOIN salaries s2 ON t1.manager_no = s2.emp_no WHERE t1.emp_salary > (SELECT s3.salary FROM salaries s3 JOIN dept_manager dmm USING (emp_no) WHERE t1.dept_no = dmm.dept_no )
思路:
1.筛选条件: 员工工资 大于 员工所在部门的manager工资
—> 员工所在部门:用correlated subqueries
—> manager 工资:
SELECT s.salary FROM salaries s JOIN dept_manager dmm USING (emp_no)
2.所使用的table:因为table中含有两个salary,所以我认为在FROM clause中要先有一个table t1,再与salaries JOIN生成最终使用的table
———>table t1: 需要包含结果中的前三列,因此要JOIN条件中所有的tables,同时排除掉manager的行
(这样才能跟最后的manager直接比较)此处注意JOIN的时候的条件就好
SELECT de.emp_no AS emp_no, de.dept_no AS dept_no, dm.emp_no AS manager_no, s1.salary AS emp_salary FROM dept_emp de JOIN dept_manager dm USING (dept_no) JOIN salaries s1 ON de.emp_no = s1.emp_no WHERE de.emp_no <> dm.emp_no