这是我今天下午得到的一个问题:
有一个表包含员工的ID,名称和薪水,在SQL Server中获取第二高薪员工的姓名
这是我的回答,我只是用纸写的,并不确定它是完全有效的,但似乎有效:
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)
我认为这很难看,但这是我脑海中唯一的解决方案。
你能建议我更好的查询吗?
非常感谢。
答案 0 :(得分:34)
SELECT * from Employee WHERE Salary IN (SELECT MAX(Salary) FROM Employee WHERE Salary NOT IN (SELECT MAX(Salary) FFROM employee));
试试这样..
答案 1 :(得分:31)
获取可以使用的第二高工资额的员工姓名。
;WITH T AS
(
SELECT *,
DENSE_RANK() OVER (ORDER BY Salary Desc) AS Rnk
FROM Employees
)
SELECT Name
FROM T
WHERE Rnk=2;
如果Salary被编入索引,以下可能会更有效率,尤其是如果有很多员工。
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)
答案 2 :(得分:9)
这可能对您有所帮助
SELECT
MIN(SALARY)
FROM
EMP
WHERE
SALARY in (SELECT
DISTINCT TOP 2 SALARY
FROM
EMP
ORDER BY
SALARY DESC
)
我们可以通过nth
(n
}代替n > 0
来找到2
最高薪水
我们提出n = 5
答案 3 :(得分:7)
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()
会给你所有薪水第二高的员工 - 无论有多少员工有相同的最高薪水。
答案 4 :(得分:5)
另一种直观的方式是: - 假设我们想找到第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;
如果有重复工资,则可以使用最内层查询distinct。
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 :(得分:4)
以下所有查询均适用于 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;
答案 6 :(得分:3)
简单方法不使用特定于Oracle,MySQL等的任何特殊功能。
假设EMPLOYEE表具有如下数据。工资可以重复。
通过人工分析,我们可以按如下方式决定等级: -
查询
可以实现相同的结果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
首先,我们发现不同的薪水。 然后我们发现不同薪水的数量大于每一行。 这只不过是那个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;
答案 7 :(得分:3)
select MAX(Salary) from Employee WHERE Salary NOT IN (select MAX(Salary) from Employee );
答案 8 :(得分:2)
我认为你会想要使用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
也不会给您一个&#39; 2&#39;作为一个等级,如果你有多个人争取最高薪水。我已经纠正了这一点,因为DENSE_RANK
为此做了最好的工作。
答案 9 :(得分:2)
以下查询可用于查找第n个最大值,只需从第n个数字
替换2select * from emp e1 where 2 =(select count(distinct(salary)) from emp e2
where e2.emp >= e1.emp)
答案 10 :(得分:1)
试试这个
select * from
(
select name,salary,ROW_NUMBER() over( order by Salary desc) as
rownum from employee
) as t where t.rownum=2
答案 11 :(得分:1)
如果您想显示获得第二高工资的员工的姓名,请使用:
SELECT employee_name
FROM employee
WHERE salary = (SELECT max(salary)
FROM employee
WHERE salary < (SELECT max(salary)
FROM employee);
答案 12 :(得分:1)
在这里,我对面试中提出的以下场景使用了两个查询
第一种情况:
找到表中的所有第二高薪(第二高薪超过
一名员工)
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;
答案 13 :(得分:1)
大多数答案是有效的。您可以按以下方式对薪水使用补偿,
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
答案 14 :(得分:1)
尝试这个以获得相应的第n个最高薪水。
SELECT
*
FROM
emp e1
WHERE
2 = (
SELECT
COUNT(salary)
FROM
emp e2
WHERE
e2.salary >= e1.salary
)
答案 15 :(得分:1)
首先有两种方法:
使用子查询查找第二高的
SELECT MAX(salary) FROM employees
WHERE salary NOT IN (
SELECT MAX (salary) FROM employees)
但是此解决方案并不是很好,因为如果您需要找出最高的10或100,那么您可能会遇到麻烦。所以取而代之的是像
select * from
(
select salary,ROW_NUMBER() over(
order by Salary desc) as
rownum from employees
) as t where t.rownum=2
通过这种方法,您可以毫无困难地找到第n个最高薪水。
答案 16 :(得分:1)
这是简单的查询..如果你想要第二个最小值,那么只需将max更改为min并将less(&lt;)符号更改为grater而不是(&gt;)。
select max(column_name) from table_name where column_name<(select max(column_name) from table_name)
答案 17 :(得分:1)
SELECT *
FROM TABLE1 AS A
WHERE NTH HIGHEST NO.(SELECT COUNT(ATTRIBUTE) FROM TABLE1 AS B) WHERE B.ATTRIBUTE=A.ATTRIBUTE;
答案 18 :(得分:1)
SELECT name
FROM employee
WHERE salary =
(SELECT MIN(salary)
FROM (SELECT TOP (2) salary
FROM employee
ORDER BY salary DESC) )
答案 19 :(得分:1)
我们还可以使用
select e2.max(sal), e2.name
from emp e2
where (e2.sal <(Select max (Salary) from empo el))
group by e2.name
请让我知道这种方法有什么问题
答案 20 :(得分:1)
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
答案 21 :(得分:1)
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
)
答案 22 :(得分:0)
这里还有一个使用 dense_rank()
select * from (select name, salary, dense_rank() over(order by salary desc)r from employee)) where r=n
在这里,'n' 是 2
答案 23 :(得分:0)
此查询显示薪水第二高的员工的所有详细信息
format
答案 24 :(得分:0)
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)
答案 25 :(得分:0)
试试这个
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
答案 26 :(得分:0)
针对该问题的四种解决方案的汇编:
来自员工的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;
答案 27 :(得分:0)
获得第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
答案 28 :(得分:0)
尝试使用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
);
答案 29 :(得分:0)
SELECT lastname, firstname
FROM employees
WHERE salary IN(
SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees));
以上是上面提到的代码:
它返回姓氏,后跟员工的名字 薪水小于所有员工的最高薪水,但也是没有最高工资的其他员工的最高薪水。
换句话说:它返回具有第二个最高薪水的员工的姓名。
答案 30 :(得分:0)
我在下面的这张图片中有一个这样的表,我将在“ to_user”列中找到第二大数字。
这是答案
从db.masterledger中选择MAX(to_user),其中to_user不输入(选择 MAX(to_user)FROM db.masterledger);
答案 31 :(得分:0)
Select * from employee where salary = (Select max(salary) from employee where salary not in(Select max(salary)from employee))
说明:
查询1::从没有薪水的员工中选择最高薪水(从员工中选择最高(薪水))-此查询将检索薪水第二高的
查询2:从薪水=(查询1)的员工中选择*-该查询将检索所有薪水第二高的记录(第二高薪可能有多个记录)
答案 32 :(得分:0)
简单的方法是使用 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
-对于第十名最大
答案 33 :(得分:0)
您可以使用 distinct(用于在 select 语句返回 NULL 值并删除重复项时返回 Null 值)和 Limit OFFSET 来获取第二个元素
select (select distinct(salary)
from Employee
order by salary desc
limit 1, 1)
as SecondHighestSalary;
Limit 1,1 ---> 第一个 1 表示 OFFSET,第二个 1 表示要返回的行数。
答案 34 :(得分:0)
我认为这可能是最简单的。
SELECT Name FROM Employees group BY Salary DESCENDING limit 2;
答案 35 :(得分:0)
尝试这种简单的方法
select name,salary from employee where salary =
(select max(salary) from employee where salary < (select max(salary) from employee ))
答案 36 :(得分:0)
试试这个:无论行数是什么,都会产生动态结果
SELECT * FROM emp WHERE salary = (SELECT max(e1.salary)
FROM emp e1 WHERE e1.salary < (SELECT Max(e2.salary) FROM emp e2))**
答案 37 :(得分:0)
这是一个简单的方法:
select name
from employee
where salary=(select max(salary)
from(select salary from employee
minus
select max(salary) from employee));
答案 38 :(得分:0)
SELECT `salary` AS emp_sal, `name` , `id`
FROM `employee`
GROUP BY `salary` ORDER BY `salary` DESC
LIMIT 1 , 1
答案 39 :(得分:0)
创建临时表
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
答案 40 :(得分:0)
- 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 )
答案 41 :(得分:0)
SELECT
salary
FROM
Employee
ORDER BY
salary DESC
LIMIT 1,
1;
答案 42 :(得分:0)
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;
答案 43 :(得分:0)
我们有一个像
这样的表name salary
A 10
B 30
C 20
D 40
所以我们要做的是先按降序排列40 30 20 10 =&gt;
然后我们将只采取前两个数字=&gt; 40 30
然后我们按升序排列=&gt; 30 40
然后我们将取第一个数字=&gt; 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 ;
答案 44 :(得分:0)
使用此SQL,第二个最高薪水将使用员工姓名
Select top 1 start at 2 salary from employee group by salary order by salary desc;
答案 45 :(得分:0)
SELECT MAX(Salary) FROM Employee
WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee)
答案 46 :(得分:0)
select
max(salary)
from
emp_demo_table
where
salary < (select max(salary) from emp_demo_table)
希望这可以用最简单的术语解决查询。
由于
答案 47 :(得分:0)
我想在这里发布可能最简单的解决方案。它在mysql中工作。
请检查你的结尾:
SELECT name
FROM `emp`
WHERE salary = (
SELECT salary
FROM emp e
ORDER BY salary DESC
LIMIT 1
OFFSET 1
答案 48 :(得分:-1)
试试这个:
WITH CTE AS
(
SELECT DENSE_RANK() OVER (ORDER BY SALARY DESC)RN,* FROM Users
)
SELECT * FROM CTE WHERE RN=2
答案 49 :(得分:-1)
SELECT *,DENSE_RANK() OVER (ORDER BY Salary Desc) AS Rnk
INTO #tmp1
FROM Employees
SELECT * FROM #tmp1 WHERE Rnk = 2
DROP TABLE #tmp1
SELECT Name,SALARY
FROM Employees
WHERE Salary = (SELECT MIN(Salary)
FROM (SELECT DISTINCT TOP (2) Salary
FROM Employees
ORDER BY Salary DESC) T);
答案 50 :(得分:-1)
试试这个:
SELECT min(salary)
FROM employee
WHERE salary IN (SELECT top 2 salary FROM employee ORDER BY salary DESC)
答案 51 :(得分:-1)
select MAX(Salary) from Employee WHERE Salary <> (select MAX(Salary) from Employee )
答案 52 :(得分:-2)
试试这个:
SELECT *
FROM emptable
WHERE empid IN (
SELECT sal,row_number () ( OVER partition by sal order by sal desc) RN
FROM emptable
WHERE RN=2)
答案 53 :(得分:-3)
select max(sal) , Department no. from employee where sal<max(sal)
答案 54 :(得分:-3)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
<input type="radio" name="pmt-type" id="bank">Bank Account Information (ACH)
</label>
<label>
<input type="radio" name="pmt-type" id="cc">Credit Card, Debit Card, Health Savings Account (HSA) or Financial Savings Account (FSA)
</label>
<label>
<input type="radio" name="pmt-type" id="later">I'll just deal with it later
</label>
<input type="submit">
<div id="bank-act-popup">
<h2>Please enter your bank account information:</h2>
<hr width="430px" class="center-hr"></hr>
<br>
<input type="text" name="routing" placeholder="Routing Number"><br>
<input type="text" name="act-num" placeholder="Account Number"/><br>
<input type="text" name="bank-name" placeholder="Bank Name"/><br>
<br><br>
<a class="btn" href="/payment/step9">Submit</a>
</div>
<div id="credit-card-popup">
<h2>Please enter your credit card information:</h2>
<hr width="430px" class="center-hr"></hr>
<div class='card-wrapper'></div>
<br>
<form>
<input type="text" name="number" placeholder="Card Number"><br>
<input type="text" name="name" placeholder="Full Name"/><br>
<input type="text" name="expiry" placeholder="Expiration Date"/><br>
<input type="text" name="cvc" placeholder="CVC"/>
</form>
</div>
注意:这仅适用于MYSQL