我如何查询员工表中所有员工的第二大薪水?
答案 0 :(得分:56)
试试这个:
SELECT max(salary)
FROM emptable
WHERE salary < (SELECT max(salary)
FROM emptable);
答案 1 :(得分:43)
简单回答:
SELECT sal
FROM emp
ORDER BY sal DESC
LIMIT 1, 1;
您只能获得第二个最高薪水。
如果你需要任何第3或第4或第N个值,你可以增加第一个值,然后是LIMIT (n-1)
即。第四个薪水:LIMIT 3, 1;
答案 2 :(得分:12)
其他大多数答案似乎都是特定于数据库的。
一般SQL查询应如下所示:
select
sal
from
emp a
where
N = (
select
count(distinct sal)
from
emp b
where
a.sal <= b.sal
)
where
N = any value
并且此查询应该能够在任何数据库上工作。
答案 3 :(得分:11)
尝试类似:
SELECT TOP 1 compensation FROM (
SELECT TOP 2 compensation FROM employees
ORDER BY compensation DESC
) AS em ORDER BY compensation ASC
本质:
如果工资不明显,您可以改为使用SELECT DISTINCT TOP ...
。
答案 4 :(得分:9)
也许你应该使用DENSE_RANK
。
SELECT *
FROM (
SELECT
[Salary],
(DENSE_RANK()
OVER
(
ORDER BY [Salary] DESC)) AS rnk
FROM [Table1]
GROUP BY [Num]
) AS A
WHERE A.rnk = 2
答案 5 :(得分:5)
select max(Emp_Sal)
from Employee a
where 1 = ( select count(*)
from Employee b
where b.Emp_Sal > a.Emp_Sal)
是跑步者。
答案 6 :(得分:5)
要找到员工的第二个最高薪水,
SELECT MAX(salary) FROM employee
WHERE salary NOT IN (
SELECT MAX (salary) FROM employee
)
要查找员工的第一和第二最高薪水,
SELECT salary FROM (
SELECT DISTINCT(salary) FROM employee ORDER BY salary DESC
) WHERE rownum<=2
此查询工作正常,因为我使用了
答案 7 :(得分:4)
试试这个:
SELECT
salary,
employeeid
FROM
employees
ORDER BY
salary DESC
LIMIT 2
然后得到第二行。
答案 8 :(得分:4)
//选择薪水第二高的员工姓名
SELECT name
FROM employee WHERE salary =
(SELECT MIN(salary) FROM
(SELECT TOP (2) salary
FROM employee
ORDER BY salary DESC) )
答案 9 :(得分:3)
select distinct(t1.sal)
from emp t1
where &n=(select count(distinct(t2.sal)) from emp t2 where t1.sal<=t2.sal);
输出: 输入n的值:如果你想要第二高,输入2;如果你想要5,输入n = 3
答案 10 :(得分:2)
试试这个:
select max(Emp_Sal)
from Employee a
where 1 = ( select count(*)
from Employee b
where b.Emp_Sal > a.Emp_Sal)
答案 11 :(得分:2)
select max(sal) from emp
where sal not in (select max(sal) from emp )
或强>
select max(salary) from emp table
where sal<(select max(salary)from emp)
答案 12 :(得分:2)
SELECT
TOP 1 salary
FROM
(
SELECT
TOP 2 salary
FROM
employees
) sal
ORDER BY
salary DESC;
答案 13 :(得分:2)
select max(Salary) from Employee
where Salary
not in (Select top4 salary from Employee);
因为答案如下
MAX(5,6,7,8-)
所以将显示第5个最高记录,前4个将不被视为
答案 14 :(得分:2)
select max(Salary) from Employee
where Salary
not in (Select Max(Salary) from Employee)
答案 15 :(得分:2)
select * from compensation where Salary = (
select top 1 Salary from (
select top 2 Salary from compensation
group by Salary order by Salary desc) top2
order by Salary)
这将为您提供薪水第二高的所有行,少数人可以分享
答案 16 :(得分:1)
select * from emp
where sal=(select min(sal) from
(select sal from(select distinct sal from emp order by sal desc)
where rownum<=n));
n可以是您想要看到的值......
你可以看到那个工资最高的人的所有领域* 强文 *