记录下自己的错题以及反思,以便以后复习用
【12题】> 困难
[错误答案]
select dept_no,s.emp_no, max(salary) maxSalary
from dept_emp de join salaries s on de.emp_no=s.emp_no
group by dept_no
order by dept_no
[分析] 错误代码无法运行
1.两表join
select de.emp_no, dept_no, salary
from dept_emp de join salaries s on de.emp_no=s.emp_no
order by dept_no
求每个部门的maxsalary——group by dept_no
分组后无法select emp_no(使用group by子句时,select子句中只能有聚合键、聚合函数、常数)
2.需要先将emp_no剔除,求出每部门maxsalary
select dept_no, max(salary)
from dept_emp de join salaries s on de.emp_no=s.emp_no
group by dept_no
order by dept_no
3.使用where 查询maxsalary对应emp_no
#正确答案
select dept_no,s.emp_no,salary maxSalary
from dept_emp de join salaries s on de.emp_no=s.emp_no
where (dept_no,salary) in (select dept_no,max(salary)
from dept_emp de join salaries s
on de.emp_no=s.emp_no
group by dept_no)
order by dept_no