我如何使用MYSQL找到第二高的薪水

时间:2019-02-07 07:31:40

标签: mysql sql

我将如何查询Employee表中所有雇员的第二大薪水?在MySQL中

3 个答案:

答案 0 :(得分:0)

按薪水降序排列,然后使用LIMIT

select *
from employees
order by salary desc
limit 1,1

第一个1表示跳过第一行,第二个1表示返回一行。

答案 1 :(得分:-1)

尝试一下,它应该可以工作。

SELECT name, salary
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees))

或者您也可以尝试这个。

/* looking for 2nd highest salary -- notice the '=2' */
SELECT name,salary FROM employees
WHERE salary = (SELECT DISTINCT(salary) FROM employees as e1
WHERE (SELECT COUNT(DISTINCT(salary))=2 FROM employees as e2
WHERE e1.salary <= e2.salary)) ORDER BY name

答案 2 :(得分:-1)

您可以在下面尝试-

  select name, max(salary) from employee where salary < (select max(salary) from employee)
    group by name;