代码之家  ›  专栏  ›  技术社区  ›  user5434198

无子查询的第二高工资

sql
  •  0
  • user5434198  · 技术社区  · 7 年前
    EmployeeID  EmployeeName    Department      Salary   
    ----------- --------------- --------------- ---------
    1           Nisha           Finance         40000.00
    2           John            Finance         25000.00
    3           NEo             Finance         25000.00
    4           Dan             Finance         15000.00
    5           Jstin           IT              80000.00
    6           Amy             IT              50000.00
    

    我想在不使用子查询的情况下获得第二高的工资,即50000.00?

    6 回复  |  直到 7 年前
        1
  •  3
  •   Manh Le    7 年前

    limit offset (跳过最高工资) 在MySQL中

    select * from table order by Salary desc limit 1 offset 1
    

    在SQL server中

    select * from table order by Salary desc offset 1 rows fetch next 1 row only
    

    http://sqlfiddle.com/#!9/15c32/1/0

        2
  •  3
  •   SOWJANYA Devarasetty    7 年前

    尝试下面的查询,它会工作。

    select max(e1.salary) from Employee e1,Employee e2 where e1.salary<e2.salary;
    
        3
  •  0
  •   DB Quest    7 年前

    你们不能只使用极限1,1(在第一行之后抓取第一个结果)吗。我来自MySql方面,因此sql可能需要针对sql Server进行轻微调整。

    SELECT Salary FROM table ORDER BY Salary DESC LIMIT 1,1
    
        4
  •  0
  •   avikalb    7 年前

    选择不同的薪资 按薪资描述偏移1行排序

    仅获取下一行

        5
  •  0
  •   Kaleemullah    6 年前

    SELECT TOP 1 * FROM TABLE NAME
    
        6
  •  -1
  •   Kaleemullah    6 年前

    在添加表名和列名后测试此查询

    select MAX(country) as country from users where country < ( select MAX(country) as country from users where country < (SELECT MAX(country) from users ) );