查询找到至少五名受薪员工

时间:2018-04-09 06:10:16

标签: sql oracle greatest-n-per-group

EMPID NAME DEPTID SALARY
---------- ------------------------------------------ --
101 surendra 201 1000
102 narendra 202 2000
103 rajesh 203 3000
104 ramesh 203 2000
105 hanumanth 202 10000

a)写一个查询以找到至少第5名(从第5位开始的最低工资) 工资最低的薪水?受薪员工?

b)查询以查找每个部门中收入最高的员工

2 个答案:

答案 0 :(得分:0)

a)写一个查询,找到至少第5名(从订单中的最低工资中获得第5名的最低工资)受薪员工?

SELECT EMPID,NAME,DEPTID,SALARY
FROM
(
SELECT EMPID,NAME,DEPTID,SALARY, DENSE_RANK() OVER (ORDER BY SALARY) AS RN
FROM Table1
) 
WHERE RN=5

b)查询以查找每个部门中收入最高的员工

SELECT EMPID,NAME,DEPTID,SALARY
FROM
(
SELECT EMPID,NAME,DEPTID,SALARY, 
  DENSE_RANK() OVER (PARTITION BY DEPTID ORDER BY SALARY DESC) AS RN
FROM Table1
)
WHERE RN=1

演示

  

http://sqlfiddle.com/#!4/63ce0/12

答案 1 :(得分:-1)

假设我们有一个名为EmployeeSalary的表,它有3列:

  1. Id
  2. 名称
  3. 薪酬
  4. 根据以下表格数据,多名员工的薪水可能相同

    Id  Name    Salary
    ----------
    6   Belalo  74
    1   Karim   100
    5   dIPU    100
    4   Satter  102
    9   Kiron   120
    10  Rash    120
    11  Harun   130
    13  Baki    130
    12  Munshi  132
    2   Rahim   500
    7   Kaif    987
    8   Sony    987
    3   Belal   4000
    

    现在查询将是

    SELECT A.Salary
    FROM
    (
    SELECT DISTINCT Salary, 
    DENSE_RANK() OVER (ORDER BY Salary) AS Ranks
    FROM [dbo].[EmployeeSalary]
    ) A 
    WHERE A.Ranks=5