数据库SQL实战(牛客网):获取当前薪水第二多的员工的emp_no以及其对应的薪水salary

获取当前(to_date=‘9999-01-01’)薪水第二多的员工的emp_no以及其对应的薪水salary
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));

--order by + group by 
select emp_no , salary
from salaries 
where to_date = '9999-01-01'
group by salary
order by salary desc
limit 1,1;
-- order by + 子查询distinct 
select emp_no , salary
from salaries 
where salary = (
select distinct salary 
from salaries 
where to_date = '9999-01-01' 
order by salary desc 
limit 1,1);
发布了292 篇原创文章 · 获赞 73 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43777983/article/details/104448879