查找当前薪水(to_date=‘9999-01-01’)排名第二多的员工编号emp_no、薪水salary、last_name以及first_name,你可以不使用order by完成吗
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_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`));
1、order by
select emp_no,salary
from salaries
where to_date='9999-01-01'
order by salary desc
limit 1,1;
2、嵌套max()函数
select e.emp_no,max(salary),last_name,first_name
from employees e, salaries s
where e.emp_no = s.emp_no
and s.to_date='9999-01-01'
and salary != (select max(salary) as salary from salaries);
3、通过查看工资第N高
select e.emp_no,salary,last_name,first_name
from employees e,salaries s
where e.emp_no = s.emp_no
and s.to_date='9999-01-01'
and salary =(select s1.salary from salaries s1,salaries s2where s1.salary<=s2.salarygroup by s1.salaryhaving count(distinct s2.salary)=2
);