正确做法:
#select max(salary) from salaries;#先找薪水第一多 #select max(salary) from salaries #where salary<(select max(salary) from salaries);#再找第二多 #最后找薪水等于第二多的。 select e.emp_no,salary,last_name,first_name from employees as e join salaries as s on e.emp_no = s.emp_no where salary=(select max(salary) from salaries where salary<(select max(salary) from salaries));
错误做法:
无group by,但select中包含聚合函数以及其他字段,那么返回的字段值与聚合函数并非一一对应的。
select e.emp_no,max(salary) as salary,last_name,first_name from employees as e join salaries as s on e.emp_no=s.emp_no where salary<(select max(salary) from employees e join salaries s on e.emp_no=s.emp_no);
返回结果:emp_no=10002,salary=74057,员工编号并不是薪水第二大的10004。