how to get second highest salary department wise without using analytical functions?

前端 未结 11 2031
孤独总比滥情好
孤独总比滥情好 2021-01-06 03:08

Suppose we have 3 employees in each department.we have total 3 departments . Below is the sample source table

Emp deptno salary
A    10     1000
B    10              


        
11条回答
  •  甜味超标
    2021-01-06 04:02

    It is a pain, but you can do it. The following query gets the second highest salary:

    select t.deptno, max(t.salary) as maxs
    from table t
    where t.salary < (select max(salary)
                      from table t2
                      where t2.deptno = t.deptno
                     )
    group by t.deptno;
    

    You can then use this to get the employee:

    select t.*
    from table t join
         (select t.deptno, max(t.salary) as maxs
          from table t
          where t.salary < (select max(salary)
                            from table t2
                            where t2.deptno = t.deptno
                           )
          group by t.deptno
         ) tt
         on t.deptno = tt.deptno and t.salary = tt.maxs;
    

提交回复
热议问题