查找员工表MySQL的最大和第二个最高工资

时间:2014-02-03 05:04:24

标签: mysql sql

假设您获得了以下名为Employee的简单数据库表,该表有2列名为Employee ID和Salary:

  Employee
  Employee ID    Salary
   3            200
   4            800
   7            450

我希望写一个查询select max(salary)as max_salary,2nd_max_salary from employee

然后它应该返回

  max_salary   2nd_max_salary
   800             450

我知道如何找到第二高薪

   SELECT MAX(Salary) FROM Employee
  WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee )

或找到第n个

  SELECT FROM Employee Emp1 WHERE (N-1) = ( SELECT COUNT(DISTINCT(Emp2.Salary)) FROM Employee Emp2
  WHERE Emp2.Salary > Emp1.Salary)

但是我无法弄清楚如何将这两个结果加入到期望的结果中

31 个答案:

答案 0 :(得分:36)

您只需运行2个查询作为内部查询即可返回2列:

select
  (SELECT MAX(Salary) FROM Employee) maxsalary,
  (SELECT MAX(Salary) FROM Employee
  WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee )) as [2nd_max_salary]

SQL Fiddle Demo

答案 1 :(得分:17)

试试这个

SELECT (select max(Salary) from Employee) as MAXinmum),(max(salary) FROM Employee WHERE salary NOT IN (SELECT max(salary)) FROM Employee);

<强>(OR)

试试这个,n将是你想要返回的第n个项目

 SELECT DISTINCT(Salary) FROM table ORDER BY Salary DESC LIMIT n,1

在你的情况下

 SELECT DISTINCT(column_name) FROM table_name ORDER BY column_name DESC limit 2,1;

答案 2 :(得分:10)

获取第二个最高工资的最简单方法第n个工资

select 
 DISTINCT(salary) 
from employee 
 order by salary desc 
limit 1,1

注意:

limit 0,1  - Top max salary

limit 1,1  - Second max salary

limit 2,1  - Third max salary

limit 3,1  - Fourth max salary

答案 3 :(得分:10)

The Best&amp;最简单的解决方案: -

 SELECT
    max(salary)
FROM
    salary
WHERE
    salary < (
        SELECT
            max(salary)
        FROM
            salary
    );

答案 4 :(得分:3)

您可以编写2个子查询,例如此示例

SELECT (select max(Salary) from Employee) as max_id, 
     (select Salary from Employee order by Salary desc limit 1,1) as max_2nd 

答案 5 :(得分:2)

我认为,这是查找MAX和第二MAX薪资的最简单方法。您可以尝试这种方式。

SELECT MAX(Salary) FROM Employee; -- For Maximum Salary.

SELECT MAX(Salary) FROM Employee WHERE Salary < (SELECT MAX(Salary) FROM Employee); -- For Second Maximum Salary

答案 6 :(得分:2)

$q="select * from info order by salary desc limit 1,0"; // display highest 2 salary

$q="select * from info order by salary desc limit 1,0"; // display 2nd highest salary

答案 7 :(得分:1)

您可以在任何您喜欢的数据库中编写SQL查询,例如 MySQL Microsoft SQL Server Oracle 。您还可以使用数据库特定功能,例如TOP,LIMIT或ROW_NUMBER编写SQL查询,但您还必须提供适用于所有数据库的通用解决方案。事实上,有几种方法可以找到第二高的薪水,你必须知道其中几个,例如在MySQL中没有使用 LIMIT 关键字,在SQL Server中没有使用 TOP 而在Oracle中没有使用 RANK ROWNUM

通用SQL查询:

SELECT
    MAX(salary)
FROM
    Employee
WHERE
    Salary NOT IN (
        SELECT
            Max(Salary)
        FROM
            Employee
    );

使用子查询而不是NOT IN子句的另一种解决方案。它使用<运算符。

SELECT
    MAX(Salary)
FROM
    Employee
WHERE
    Salary < (
        SELECT
            Max(Salary)
        FROM
            Employee
    );

答案 8 :(得分:1)

试试这个

select max(salary) as first, 
       (select salary from employee order by salary desc limit 1, 1) as second 
from employee limit 1

答案 9 :(得分:1)

`select max(salary) as first, (select salary from employee order by salary desc limit 1, 1) as second from employee limit 1`

对于最高工资,我们可以使用max函数,但是第二个最高工资我们应该使用子查询。在子查询中,我们可以使用where条件来检查第二个最高工资,或者只是我们可以使用限制。

答案 10 :(得分:1)

我认为oracle中的简单方法是:

SELECT Salary FROM
(SELECT DISTINCT Salary FROM Employee ORDER BY Salary desc)
WHERE ROWNUM <= 2;

答案 11 :(得分:0)

select * from
  Employees where Sal >=
  (SELECT
    max(Sal)
FROM
    Employees
WHERE
    Sal < (
        SELECT
            max(Sal)
        FROM
            Employees; 
    ));

答案 12 :(得分:0)

最高工资:

select max(salary) from tbl_employee <br><br>

第二最高工资:

select max(salary) from tbl_employee where salary < ( select max(salary) from tbl_employee);

答案 13 :(得分:0)

此解决方案将提供薪水第二高的所有员工姓名和薪水

SELECT name, salary
FROM employee
WHERE salary = (
    SELECT 
        salary
    FROM employee AS emp
    ORDER BY salary DESC
    LIMIT 1,1
);

答案 14 :(得分:0)

select * from emp where sal =(select max(sal) from emp where eno in(select eno from emp where sal <(select max(sal)from emp )));

尝试以上代码......

如果你想要第三个最大记录,那么添加另一个嵌套查询&#34;从emp&#34;中选择max(sal)在最后一个查询的括号内,并在它前面给出少于操作符。

答案 15 :(得分:0)

  

mysql中最高的3个薪水

SELECT Salary FROM (SELECT DISTINCT Salary FROM Employee ORDER BY Salary desc) AS custom LIMIT 3;
  

mysql中最高的n个薪水,只需输入n的值

SELECT Salary FROM (SELECT DISTINCT Salary FROM Employee ORDER BY Salary desc) AS custom LIMIT n;

答案 16 :(得分:0)

这是另一个使用子查询的解决方案,但它使用&lt;而不是IN子句。操作

SELECT MAX(Salary) From Employees WHERE Salary < ( SELECT Max(Salary) FROM Employees);

答案 17 :(得分:0)

select e.salary from( SELECT * FROM Employee group by salary order by salary desc limit 2 ) e order by salary asc limit 1;

答案 18 :(得分:0)

这样可以找到第n个最大数

SELECT 
    TOP 1 * from (SELECT TOP  nth_largest_no * FROM Products Order by price desc) ORDER BY price asc;

对于第五大数字

SELECT 
  TOP 1 *  from (SELECT TOP  5 * FROM Products Order by price desc) ORDER BY price asc;

答案 19 :(得分:0)

根据您的要求更改n值:

SELECT top 1 amount 
FROM ( 
    SELECT DISTINCT top n amount 
    FROM payment 
    ORDER BY amount DESC ) AS temp 
ORDER BY amount

答案 20 :(得分:0)

如果我们想找到获得第三高薪的员工,那么执行此查询

SELECT a.employeeid, 
       a.salary 
FROM  (SELECT employeeid, 
              salary, 
              Dense_rank() 
                OVER( 
                  ORDER BY salary) AS n 
       FROM   employee) AS a 
WHERE  n = 3 

你想要什么?

答案 21 :(得分:0)

尝试以下查询,正在为我找到第N个最高号码的工资。 只需用 nth_No

替换您的号码即可
Select DISTINCT TOP 1 salary
from 
(Select DISTINCT TOP *nth_No* salary
from Employee
ORDER BY Salary DESC)
Result
ORDER BY Salary

答案 22 :(得分:0)

这应该是相同的:

SELECT MAX(salary) max_salary,
  (SELECT MAX(salary)
   FROM Employee
   WHERE salary NOT IN
       (SELECT MAX(salary)
        FROM Employee)) 2nd_max_salary
FROM Employee

答案 23 :(得分:0)

没有嵌套查询

select max(e.salary) as max_salary, max(e1.salary) as 2nd_max_salary 
from employee as e
left join employee as e1 on e.salary != e1.salary
group by e.salary desc limit 1;

答案 24 :(得分:0)

使用公用表表达式

With cte as (
 SELECT 
   ROW_NUMBER() Over (Order By Salary Desc) RowNumber, 
   Max(Salary) Salary 
 FROM
     Employee
 Group By Salary
)

Select * from cte where RowNumber = 2

答案 25 :(得分:0)

这是一个很棒的查询来查找第n个最大值: 例如: -

  1. 您想找工资第8行薪资,只需将索引值更改为8。

  2. 假设您有100行Salary。现在你要找到第90行的最高工资。刚刚将索引值更改为90。

    set @n:=0;
    select * from (select *, (@n:=@n+1) as indexed from employee order by Salary desc)t where t.indexed = 1;
    

答案 26 :(得分:0)

第二高薪,这个对我有用:

SELECT salary
FROM employee
WHERE salary 
NOT IN (
SELECT MAX( salary ) 
FROM employee
ORDER BY salary DESC
)
LIMIT 1

答案 27 :(得分:0)

尝试

SELECT 

   SUBSTRING( GROUP_CONCAT( Salary ), 1 , LOCATE(",", GROUP_CONCAT( Salary ) ) -1 ) AS max_salary,

   SUBSTRING( GROUP_CONCAT( Salary ), LOCATE(",", GROUP_CONCAT( Salary ) ) +1 ) AS second_max_salary

FROM 
   (
     SELECT Salary FROM `Employee` ORDER BY Salary DESC LIMIT 0,2
   ) a

演示here

答案 28 :(得分:0)

对于独特的工资(即首先不能与第二个相同):

SELECT
  MAX( s.salary ) AS max_salary,
  (SELECT
     MAX( salary ) 
   FROM salaries
   WHERE salary <> MAX( s.salary ) 
   ORDER BY salary DESC 
   LIMIT 1
  ) AS 2nd_max_salary
FROM salaries s

还因为这是解决这个问题的一种不必要的方法(任何人都可以说2行而不是2列,LOL?)

答案 29 :(得分:0)

不是很好的查询,但是:

SELECT * from (
    SELECT max(Salary) from Employee
    ) as a
LEFT OUTER JOIN 
    (SELECT MAX(Salary) FROM Employee
        WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee )) as b
ON 1=1

答案 30 :(得分:-1)

Select Distinct sal From emp Order By sal Desc Limit 1,1;

将需要所有Distinct sal。并且Limit 1,1表示:留下前一条记录并打印1条记录。