1. 程式人生 > >資料庫SQL實戰 --15.獲取當前薪水第二多的員工的emp_no以及其對應的薪水salary

資料庫SQL實戰 --15.獲取當前薪水第二多的員工的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`));

解決思路

select emp_no, salary from salaries
where to_date = '9999-01-01' and 
salary = (select distinct salary from salaries order by salary desc limit 1,1)
  • 注意用distinct去重,不然查詢處理結果可能有多個