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

前端 未结 11 1866
孤独总比滥情好
孤独总比滥情好 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 03:47

    On MySQL this how you can get second highest salary, given table name is salaries:

    By Nested Queries: (where you can change offset 0/1/2 for first, second and third place respectively)

    select
      *
    from
      salaries as t1 
    where 
      t1.salary = (select 
                   salary
                 from 
                   salaries
                 where 
                 salaries.deptno = t1.deptno ORDER by salary desc limit 1 offset 1);
    

    or might be by creating rank: (where you can change rank= 1/2/3 for first, second and third place respectively)

    SET @prev_value = NULL;
    SET @rank_count = 0;
    select * from 
    (SELECT
      s.*, 
      CASE 
          WHEN @prev_value = deptno THEN @rank_count := @rank_count + 1
          WHEN @prev_value := deptno THEN @rank_count := 1 
          ELSE @rank_count := 1 
      END as rank
    FROM salaries s
    ORDER BY deptno, salary desc) as t
    having t.rank = 2;
    
    0 讨论(0)
  • 2021-01-06 03:51

    This will give you 2nd highest salary in each department:

    SELECT a.Emp, a.deptno, a.salary
    FROM Emp a
    WHERE 1 = (SELECT COUNT(DISTINCT salary) 
            FROM Emp b 
            WHERE b.salary > a.salary AND a.deptno = b.deptno)
    group by a.deptno
    
    0 讨论(0)
  • 2021-01-06 03:51

    SQL Query:

    select TOP 2 max(salary),Emp from EMployee where deptno='your_detpno'
    
    0 讨论(0)
  • 2021-01-06 03:56
    select min(salary),deptno from
    (SELECT distinct top 2 salary,deptno from table ORDER BY salary DESC)
    as  a 
    group by deptno
    
    0 讨论(0)
  • 2021-01-06 03:59

    You can find 2nd highest salary something like this:

    select max(a.Salary),a.Deptno from Employee a join (select MAX(salary) salary 
    from Employee group by Deptno) b on a.Salary < b.salary group by a.Deptno
    

    And no MAX() is not an analytic function.

    Reference

    0 讨论(0)
  • 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;
    
    0 讨论(0)
提交回复
热议问题