it-swarm.cn

如何获得表中薪水第二高的员工

我今天下午有一个问题:

在SQL Server中,有一个表包含ID,姓名和员工薪水,获取薪水第二高的员工的姓名

这是我的答案,我只是将其写在纸上,不确定它是否完全有效,但似乎可行:

SELECT Name FROM Employees WHERE Salary = 
( SELECT DISTINCT TOP (1) Salary FROM Employees WHERE Salary NOT IN
 (SELECT DISTINCT TOP (1) Salary FROM Employees ORDER BY Salary DESCENDING)
ORDER BY Salary DESCENDING)

我认为这很丑陋,但这是我想到的唯一解决方案。

您能建议我一个更好的查询吗?

非常感谢你。

35
Vimvq1987

要获取具有第二高不同薪水金额的雇员的姓名,可以使用。

;WITH T AS
(
SELECT *,
       DENSE_RANK() OVER (ORDER BY Salary Desc) AS Rnk
FROM Employees
)
SELECT Name
FROM T
WHERE Rnk=2;

如果将薪水编入索引,则以下内容可能会更有效率,尽管在有很多员工的情况下尤其如此。

SELECT Name
FROM   Employees
WHERE  Salary = (SELECT MIN(Salary)
                 FROM   (SELECT DISTINCT TOP (2) Salary
                         FROM   Employees
                         ORDER  BY Salary DESC) T);

测试脚本

CREATE TABLE Employees
  (
     Name   VARCHAR(50),
     Salary FLOAT
  )

INSERT INTO Employees
SELECT TOP 1000000 s1.name,
                   abs(checksum(newid()))
FROM   sysobjects s1,
       sysobjects s2

CREATE NONCLUSTERED INDEX ix
  ON Employees(Salary)

SELECT Name
FROM   Employees
WHERE  Salary = (SELECT MIN(Salary)
                 FROM   (SELECT DISTINCT TOP (2) Salary
                         FROM   Employees
                         ORDER  BY Salary DESC) T);

WITH T
     AS (SELECT *,
                DENSE_RANK() OVER (ORDER BY Salary DESC) AS Rnk
         FROM   Employees)
SELECT Name
FROM   T
WHERE  Rnk = 2;

SELECT Name
FROM   Employees
WHERE  Salary = (SELECT DISTINCT TOP (1) Salary
                 FROM   Employees
                 WHERE  Salary NOT IN (SELECT DISTINCT TOP (1) Salary
                                       FROM   Employees
                                       ORDER  BY Salary DESC)
                 ORDER  BY Salary DESC)

SELECT Name
FROM   Employees
WHERE  Salary = (SELECT TOP 1 Salary
                 FROM   (SELECT TOP 2 Salary
                         FROM   Employees
                         ORDER  BY Salary DESC) sel
                 ORDER  BY Salary ASC)  
31
Martin Smith
从员工
中选择* *薪水在哪里(SELECT MAX(薪水)
从员工
在哪里薪水不在(SELECT MAX(薪水)
 FFROM员工)); 

像这样尝试.

34
amateur

这可能对您有帮助

SELECT 
      MIN(SALARY) 
FROM 
      EMP 
WHERE 
      SALARY in (SELECT 
                      DISTINCT TOP 2 SALARY 
                 FROM 
                      EMP 
                 ORDER BY 
                      SALARY DESC
                )

我们可以用n(其中nth)代替n > 0来找到2的最高薪水

5的例子 我们投入的最高薪水n = 5

10
meekash55

CTE怎么样?

;WITH Salaries AS
(
    SELECT Name, Salary,
       DENSE_RANK() OVER(ORDER BY Salary DESC) AS 'SalaryRank'
    FROM 
        dbo.Employees
)
SELECT Name, Salary
FROM Salaries  
WHERE SalaryRank = 2

DENSE_RANK()会给您所有薪水第二高的员工-不管有多少(薪水)最高的员工。

7
marc_s

另一个直观的方法是:-假设我们想找到第N个最高薪水,然后

1)按薪金降序对员工进行排序

2)使用rownum获取前N条记录。因此,在此步骤中,第N个记录是第N个最高薪水

3)现在按升序对临时结果进行排序。因此,第N高的薪水现在是第一记录

4)从此临时结果中获取第一条记录。

这将是第N高的薪水。

select * from 
 (select * from 
   (select * from  
       (select * from emp order by sal desc)  
   where rownum<=:N )  
 order by sal )
where rownum=1;

如果存在重复的薪水,则在最内部的查询中可以使用不同的薪水。

select * from 
 (select * from 
   (select * from  
       (select distinct(sal) from emp order by 1 desc)  
   where rownum<=:N )  
 order by sal )
where rownum=1;
5
Kaushik Lele

以下所有查询均适用于MySQL

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

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

SELECT Salary FROM Employee ORDER BY Salary DESC LIMIT 1 OFFSET 1;

SELECT Salary FROM (SELECT Salary FROM Employee ORDER BY Salary DESC LIMIT 2) AS Emp ORDER BY Salary LIMIT 1;
4
Pratik Patil
select MAX(Salary) from Employee WHERE Salary NOT IN (select MAX(Salary) from Employee );
4
steave

无需使用任何特定于Oracle,MySQL等的特殊功能的简单方法.

假设EMPLOYEE表具有以下数据。工资可以重复。 enter image description here

通过手工分析,我们可以决定等级如下:
enter image description here

通过查询可以达到相同的结果

select  *
from  (
select tout.sal, id, (select count(*) +1 from (select distinct(sal) distsal from     
EMPLOYEE ) where  distsal >tout.sal)  as rank  from EMPLOYEE tout
) result
order by rank

enter image description here

首先,我们找出不同的薪水。然后我们找出比每一行都多的不同薪水的计数。这不过是该ID的等级。对于最高薪水,此计数将为零。因此,执行“ +1”可以从1开始排名。

现在,通过在上述查询中添加where子句,我们可以获得第N级的ID。

select  *
from  (
select tout.sal, id, (select count(*) +1 from (select distinct(sal) distsal from     
EMPLOYEE ) where  distsal >tout.sal)  as rank  from EMPLOYEE tout
) result
where rank = N;
3
Kaushik Lele

下面的查询可用于查找第n个最大值,只需将第n个数字替换为2

select * from emp e1 where 2 =(select count(distinct(salary)) from emp e2
   where e2.emp >= e1.emp)
2
Gopal Sanodiya

我想您会想使用DENSE_RANK,因为您不知道有多少名员工的工资相同,并且您确实说过要使用员工姓名。

CREATE TABLE #Test
(
    Id INT,
    Name NVARCHAR(12),
    Salary MONEY
)

SELECT x.Name, x.Salary
FROM
        (
        SELECT  Name, Salary, DENSE_RANK() OVER (ORDER BY Salary DESC) as Rnk
        FROM    #Test
        ) x
WHERE x.Rnk = 2

ROW_NUMBER将为您提供唯一的编号,即使薪水并列,而普通RANK不会为您提供“ 2”作为排名,如果您有多个人在争取最高薪水。我已将其更正,因为DENSE_RANK为此做了最好的工作。

2
ericb

尝试以此获得各自的第n高薪水。

SELECT
    *
FROM
    emp e1
WHERE
    2 = (
        SELECT
            COUNT(salary)
        FROM
            emp e2
        WHERE
            e2.salary >= e1.salary
    )
1
Ajay Kumar
select max(age) from yd where age<(select max(age) from HK) ; /// True two table Highest 

SELECT * FROM HK E1 WHERE 1 =(SELECT COUNT(DISTINCT age) FROM HK E2 WHERE E1.age < E2.age); ///Second Hightest age RT single table 

select age from hk e1 where (3-1) = (select count(distinct (e2.age)) from yd e2 where e2.age>e1.age);//// same True Second Hight age RT two table

select max(age) from YD where age not in (select max(age) from YD);  //second hight age in single table 
1
H K RAO

在这里,我针对以下情况使用了两个查询,这些情况是在采访中提出的
第一种情况:
查找表中所有第二高的薪水(一名雇员以上的第二高薪)

select * from emp where salary
   In (select MAX(salary) from emp where salary NOT IN (Select MAX(salary) from 
   emp));

第二种情况:
只找到表格中第二高的薪水

select min(temp.salary) from (select * from emp order by salary desc limit 2) 
  temp;
1
Gobi

大多数答案都是有效的。您可以按如下所示将薪水与按薪金排序,

SELECT NAME
FROM EMPLOYEES
WHERE SALARY IN
(
    SELECT DISTINCT 
           SALARY
    FROM EMPLOYEES
    ORDER BY SALARY DESC
    OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY
);

要找到第n个最高薪水,请用n替换抵消1

1
Code_Mode

这是简单的查询..如果您要第二个最小值,则只需将最大值更改为min,然后将less(<<)符号更改为grater than(>)。

    select max(column_name) from table_name where column_name<(select max(column_name) from table_name)
1
anurag sethi

如果要显示获得第二高薪水的雇员的姓名,请使用以下命令:

SELECT employee_name 
FROM employee
WHERE salary = (SELECT max(salary) 
                FROM employee
                WHERE salary < (SELECT max(salary) 
                                FROM employee);
1
Manjunath N Shetty

我们也可以使用

select e2.max(sal), e2.name
from emp e2
where (e2.sal <(Select max (Salary) from empo el))
group by e2.name

请让我知道这种方法有什么问题

1
lakshmi

试试这个

    select * from
   (
    select name,salary,ROW_NUMBER() over( order by Salary desc) as
    rownum from    employee
   ) as t where t.rownum=2

http://askme.indianyouth.info/details/write-a-sql-query-to-find-the-10th-highest-employee-salary-from-an-employee-table-explain-your-答案-111

1
Harikesh Yadav
SELECT * 
FROM TABLE1 AS A 
WHERE NTH HIGHEST NO.(SELECT COUNT(ATTRIBUTE) FROM TABLE1 AS B) WHERE B.ATTRIBUTE=A.ATTRIBUTE;
1
PRASAD
select * from emp where salary = (  
    select salary from   
       (select ROW_NUMBER() over (order by salary) as 'rownum', *
        from emp) t -- Order employees according to salary  
    where rownum = 2 -- Get the second highest salary
)
1
sh_kamalh
SELECT name
FROM employee
WHERE salary =
(SELECT MIN(salary) 
  FROM (SELECT TOP (2) salary
  FROM employee
  ORDER BY salary DESC) )
1
Himalaya Garg

随便我们有一张桌子

name       salary
 A           10
 B           30
 C           20
 D           40

所以我们要做的是首先以降序排列40 30 20 10 =>

那么我们将只取前两个数字=> 40 30

然后我们以升序排列=> 30 40

那么我们将取第一个数字=> 30

所以在mysql ::

SELECT * FROM (SELECT  * FROM employee   order by salary DESC LIMIT 2) order by salary ASC LIMIT 1;

在Oracle中::

SELECT * FROM (SELECT  * FROM employee  where rownum<=2 order by salary DESC )  where rownum<=1  order by salary ASC ;
0
Ritabrata Gautam
SELECT `salary` AS emp_sal, `name` , `id`
FROM `employee`
GROUP BY `salary` ORDER BY `salary` DESC
LIMIT 1 , 1 
0
mayurjadhav
SELECT MIN(a.sal) 
FROM dbo.demo a 
WHERE a.sal IN (SELECT DISTINCT TOP 2 a.sal 
                FROM dbo.demo a 
                ORDER BY a.sal DESC) 
0
pintu kumar panda

此查询显示工资第二高的员工的所有详细信息

SELECT
    *
FROM
    Employees
WHERE
    salary IN (
        SELECT
            max(salary)
        FROM
            Employees
        WHERE
            salary NOT IN (
                SELECT
                    max(salary)
                FROM
                    Employees
            )
    );
0
Somex Gupta

针对该问题的四个解决方案的汇编:

第一个解决方案-使用子查询

雇员的SELECT MAX(薪水)----此查询将为您提供最高薪水

现在使用上面的查询作为子查询来获得下一个最高薪水,如下所示:

SELECT MAX(salary) FROM employee WHERE salary <> (SELECT MAX(salary) from Employee)-此查询将为您提供第二高的薪水

现在,如果您想获得薪水第二高的雇员的姓名,则使用上面的查询作为子查询来获取它,如下所示

SELECT name from employee WHERE salary = 
(SELECT MAX(salary) FROM employee WHERE salary <> (SELECT MAX(salary) from Employee)

-此查询将为您提供第二高的薪水)

第二种解决方案-使用派生表

SELECT TOP 2 DISTINCT(salary) FROM employee ORDER BY salary DESC-这将给您两个最高的薪水。您在这里要做的是按降序排列薪水,然后选择前2名薪水。

现在按薪水升序排列上述结果集,并获得TOP 1

SELECT TOP 1 salary FROM
(SELECT TOP 2 DISTINCT(salary) FROM employee ORDER BY salary DESC) AS tab 
ORDER BY salary

第三种解决方案-使用相关子查询

SELECT name, salary FROM Employee e WHERE 2=(SELECT COUNT(DISTINCT salary) FROM Employee p WHERE e.salary<=p.salary)

第四个解决方案-使用窗口功能

;WITH T AS
(
SELECT *, DENSE_RANK() OVER (ORDER BY Salary Desc) AS Rnk
FROM Employees
)
SELECT Name
FROM T
WHERE Rnk=2;
0
Dipendu Paul
 - Method 1

    select max(salary) from Employees
        where salary< (select max(salary) from Employees)



 - Method 2

 select MAX(salary) from Employees 
    where salary not in(select MAX(salary) from Employees)



 - Method 3

select MAX(salary) from Employees 
    where salary!= (select MAX(salary) from Employees )
0
Tuscan
SELECT lastname, firstname
FROM employees
WHERE salary IN(
    SELECT MAX(salary) 
    FROM employees 
    WHERE salary < (SELECT MAX(salary) FROM employees));

所以这是上面提到的代码的作用:

它返回姓氏,后跟雇员的名字,这些雇员的薪水小于所有雇员的最高薪水,但它也是没有最高薪水的其余雇员的最高薪水。

换句话说:它返回第二高薪的雇员的姓名。

0
Leivadaros Paraskevas

简单的方法是使用OFFSET。不仅第二,我们可以使用偏移量查询的任何位置。

SELECT SALARY,NAME FROM EMPLOYEE ORDER BY SALARY DESC LIMIT 1 OFFSET 1 --第二最大

SELECT SALARY,NAME FROM EMPLOYEE ORDER BY SALARY DESC LIMIT 1 OFFSET 9-对于第10个最大

0
REMITH

试试这个简单的方法

select name,salary from employee where salary =
(select max(salary) from employee where salary < (select max(salary) from employee ))
0
Vilas Galave

寻找第二高的薪水...

SELECT MAX( salary)  FROM tblEmp WHERE salary< ( SELECT MAX( salary) FROM tblEmp )

要么

SELECT max(salary) FROM tblEmp WHERE salary NOT IN (SELECT max(salary) FROM tblEmp)

其中“ salary”是列名,而tblEmp是表名...两者都可以100%工作...

0
Durgesh Pandey

我认为这可能是最简单的方法。

SELECT Name FROM Employees group BY Salary DESCENDING limit 2;
0
Abhilash
declare

cntr number :=0;

cursor c1 is

select salary from employees order by salary desc;

z c1%rowtype;

begin

open c1;

fetch c1 into z;

while (c1%found) and (cntr <= 1) loop


cntr := cntr + 1;

fetch c1 into z;

dbms_output.put_line(z.salary);

end loop;

end;
0
Ashish sinha

创建临时表

Create Table #Employee (Id int identity(1,1), Name varchar(500), Salary int)

插入资料

Insert Into #Employee
    Select 'Abul', 5000
Union ALL 
    Select 'Babul', 6000
Union ALL 
    Select 'Kabul', 7000
Union ALL 
    Select 'Ibul', 8000
Union ALL 
    Select 'Dabul', 9000

查询将是

select top 1 * from #Employee a
Where a.id <> (Select top 1 b.id from #Employee b ORDER BY b.Salary desc)
order by a.Salary desc

放下桌子

drop table #Empoyee
0
amnippon
SELECT
    salary
FROM
    Employee
ORDER BY
    salary DESC
LIMIT 1,
 1;
0
Kiran

这是一个简单的方法:

select name
from employee
where salary=(select max(salary)
              from(select salary from employee
                   minus
                   select max(salary) from employee));
0
Vikas Bansal

使用此SQL,将以员工姓名获得第二高的薪水

Select top 1 start at 2 salary from employee group by salary order by salary desc;
0
Deepraj Shetty
select 
    max(salary) 
from 
    emp_demo_table 
where 
    salary < (select max(salary) from emp_demo_table)

希望这可以用最简单的术语解决查询。

谢谢

0
Ash

我在下面的这张图片中有一个这样的表,我将在“ to_user”列中找到第二大数字。 enter image description here

这是答案

选择MAX(to_user)FROM db.masterledger,其中to_user NOT IN(SELECT MAX(to_user)FROM db.masterledger);

0
Vignesh R

Select * from employee where salary = (Select max(salary) from employee where salary not in(Select max(salary)from employee))

说明:

  • 查询1:从没有薪水的员工中选择最高薪水(从员工中选择最高(薪水))-此查询将检索第二高薪水

  • 查询2:从员工那里选择*薪水=(查询1)-此查询将检索所有薪水第二高的记录(第二高薪可能有多个记录)

0
vinod wakade
SELECT MAX(Salary) FROM Employee
WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee)
0
Avinash Singh

我想在这里发布最简单的解决方案。它在mysql中工作。

请也检查您的结尾:

SELECT name
FROM `emp`
WHERE salary = (
SELECT salary
FROM emp e
ORDER BY salary DESC
LIMIT 1
OFFSET 1 
0
Vandana

为MSSQL尝试以下方法:

SELECT
    TOP 1 salary
FROM
    (
        SELECT
            TOP 2 salary
        FROM
            Employees 
    ) sal
ORDER BY
    salary DESC;

但是,您应该尝试适用于所有数据库的通用SQL查询。

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

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

薪水排名第n位。简单的方法

select t.name,t.sal
from (select name,sal,dense_rank() over (order by sal desc) as rank from emp) t
where t.rank=6; //suppose i find 6th highest salary
0
NAUSHAD AHAMAD

尝试这个

select * from (
   select ROW_NUMBER() over (order by [salary] desc) as sno,emp_name,   
   [salary] from [dbo].[Emp]
) t 
where t.sno =10

with t as
select top (1) * from       
  (select top (2) emp_name,salary from   [Emp]  e
   order by  salary desc) t
order  by salary asc
0
Soumya brata Banerjee

尝试此操作:无论行数如何,都将提供动态结果

SELECT * FROM emp WHERE salary = (SELECT max(e1.salary) 
FROM emp e1 WHERE e1.salary < (SELECT Max(e2.salary) FROM emp e2))**
0
Jyo