【问题标题】:how to find third highest salary [closed]如何找到第三高的薪水[关闭]
【发布时间】:2015-05-08 10:23:02
【问题描述】:

如何使用sql查询从表中找出第三高的薪水? 请通过显示查询来举例说明

【问题讨论】:

标签: sql


【解决方案1】:
select MIN(salary) from employes where salary in 
(select TOP 3 salary from employes order by salary desc)

【讨论】:

    【解决方案2】:

    您可以使用 CTE 和 Row_Number 获得第三高的薪水

    WITH CTE AS
    (
        SELECT EmpID,EmpName,EmpSalar,
               RN = ROW_NUMBER() OVER (ORDER BY EmpSalary DESC)
        FROM dbo.Salary
    )
    SELECT EmpID,EmpName,EmpSalar
    FROM CTE
    WHERE RN = 3
    

    【讨论】:

    • 排名功能ROW_NUMBER是最好的选择!
    • 在我看来不是一个通用的解决方案..
    • @MilenPavlov,你说“不是通用解决方案”是什么意思?
    • 未指定 rdbms,因此这将在 SQL Server 中工作,但在 MySql 中不起作用 :)
    【解决方案3】:

    使用标准 SQL,您可以:

    select salary
    from (select distinct salary
          from table t
         ) t
    order by salary desc
    offset 2
    fetch next 1 row ;
    

    【讨论】:

      【解决方案4】:
      select Max(salary)
      from data t3
      where t3.salary < 
          (
            select Max(t1.salary) 
            from data t1 
             where t1.salary < (select 
                                Max(t2.Salary) 
                                from data t2)
           )
      

      Demo here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-29
        • 2011-01-30
        • 2014-09-02
        • 2019-05-04
        • 1970-01-01
        • 2011-12-20
        • 1970-01-01
        • 2019-04-17
        相关资源
        最近更新 更多