Nth highest Salary.

I need a well tuned query to find the nth highest salary from emp table.
I have one but no idea if it is tuned one.
select min(sal) from (select sal,rownum from (select sal from (select distinct sal from emp order by sal desc)) where rownum <=n) ;

You can use analytic functions.
select *
  from (
select e.*, row_number() over(order by sal desc nulls last) rno
  from emp e
where rno = 5 --<<-- replace 5 with N
I need a well tuned query to find the nth highest salary from emp table.This totally depends on how you have setup your database.

Similar Messages

  • Finding nth highest salary

    Hi guys , I am trying to find the nth highest salary
    using the below query , is there any case in which the below query can fail
    select
    * from (
    select
    distinct rownum rn,salary from emp_mgr order by rownum) t where t.rn=3

    chris227 wrote:
    Sory, may be i am blind, dont see it yet.
    Doesnt the Nth high paid employee earns the Nth highest salary?
    No. Multiple employees can earn same salary. Look at EMP table:
    SQL> select ename,sal from emp order by sal desc;
    ENAME             SAL
    KING             5000
    FORD             3000
    SCOTT            3000
    JONES            2975
    BLAKE            2850
    CLARK            2450
    ALLEN            1600
    TURNER           1500
    MILLER           1300
    WARD             1250
    MARTIN           1250
    ENAME             SAL
    ADAMS            1100
    JAMES             950
    SMITH             800
    Highest salary 5000. Second highest salary is 3000. Third highest salary is 2975. Now look what NTH_VALUE returns:
    SQL> select distinct sal
      2    from (select nth_value(sal,&n) over(order by sal desc) sal from emp)
      3  /
    Enter value for n: 1
    old   2:   from (select nth_value(sal,&n) over(order by sal desc) sal from emp)
    new   2:   from (select nth_value(sal,1) over(order by sal desc) sal from emp)
           SAL
          5000
    SQL> /
    Enter value for n: 2
    old   2:   from (select nth_value(sal,&n) over(order by sal desc) sal from emp)
    new   2:   from (select nth_value(sal,2) over(order by sal desc) sal from emp)
           SAL
          3000
    SQL> /
    Enter value for n: 3
    old   2:   from (select nth_value(sal,&n) over(order by sal desc) sal from emp)
    new   2:   from (select nth_value(sal,3) over(order by sal desc) sal from emp)
           SAL
          3000
    SQL> /
    Enter value for n: 4
    old   2:   from (select nth_value(sal,&n) over(order by sal desc) sal from emp)
    new   2:   from (select nth_value(sal,4) over(order by sal desc) sal from emp)
           SAL
          2975
    SQL>
    SY.

  • Nth Highest Salary from EMP table , need explaination

    I am new to the database world and hence learning queries at the initial stages. I want to retrieve the nth highest salary from the EMP table. I was not able to find out the answer by myself , so I searched on google. The following was the answer i got :
    SELECT DISTINCT (a.sal) FROM EMP A WHERE &N = (SELECT COUNT (DISTINCT (b.sal)) FROM EMP B WHERE a.sal<=b.sal);
    Enter value for n: 2
    SAL
    3700
    Suppose the table contains salary in the following order :
    SAL
    2000
    3000
    3700
    4000
    3700
    2000
    So how come I will get the correct answer with the abov query ? I am not able to understand the part --- WHERE &N = (SELECT COUNT (DISTINCT (b.sal)) FROM EMP B WHERE a.sal<=b.sal);
    Specially the condition WHERE a.sal<=b.sal is very confusing to me. Can anyone help me please to understand the same.

    user12328699 wrote:
    I am new to the database world and hence learning queries at the initial stages. I want to retrieve the nth highest salary from the EMP table. I was not able to find out the answer by myself , so I searched on google. The following was the answer i got :
    SELECT DISTINCT (a.sal) FROM EMP A WHERE &N = (SELECT COUNT (DISTINCT (b.sal)) FROM EMP B WHERE a.sal<=b.sal);
    Enter value for n: 2
    SAL
    3700
    Suppose the table contains salary in the following order :
    SAL
    2000
    3000
    3700
    4000
    3700
    2000
    So how come I will get the correct answer with the abov query ? I am not able to understand the part --- WHERE &N = (SELECT COUNT (DISTINCT (b.sal)) FROM EMP B WHERE a.sal<=b.sal);
    Specially the condition WHERE a.sal<=b.sal is very confusing to me. Can anyone help me please to understand the same.There is many better way to get the nth highest salary
    But still if you want to understand the same.
    Assuming the data is sorted in ascending order, then
    1) a.sal=2000, so a.sal<=b.sal results in count as 3 (not equal to user input)
    2) a.sal=3700, so a.sal<=b.sal results in count as 2 (equal to user input)
    3) a.sal = 4000 , so a.sal<=b.sal results in count as 1 (not equal to user input)
    Hence the answer is 3700
    Regards
    Anurag

  • Pls help me in finding highest salary of employee.

    can anybody give me...
    the general query...for finding out nth highest salary of the employee in a particular department.....
    for example, if i want to find out 2nd highest salary or some 5th highest salary of the employee.....
    i want query with a littlebit explanation also...

    i
    cannot do the sumation of those value then the
    negative value that being re-enter become 0. how can
    i make the value to become another positive value?Are you doing the `sumation' during the data entry (loop)? If so, make sure you only include valid value in the count (i.e. positive integers).

  • How will i get the n'th highest salary????????

    Hi
    plz can anybody tell me the query to find nth highest salary from emp table???
    Thanx
    Gagan

    try this
    SELECT employee_name, salary,
           RANK() OVER (order by salary) AS ranking
    FROM   employee) empor to get the particular ranking
    SELECT emp.employee_name, emp.salary
    FROM
         (SELECT employee_name, salary,
                 RANK() OVER (order by salary) AS ranking
          FROM   employee) emp
    WHERE emp.ranking = <rank number>

  • Query to find Highest Salary

    Hi,
    What is the SQL query to find the 1st highest salary.
    2nd Highest salary in the emp table.
    likewise nth highest salary employee records.
    Can anyone please give me the queries for the above scenarios.
    Thanks

    There are many nethods, some examples
    Using group function:
    SQL> l
      1  SELECT ename,sal FROM emp e1
      2  WHERE &val >= (SELECT COUNT(*) FROM emp e2
      3*               WHERE e1.sal<=e2.sal)
    SQL> /
    Enter value for val: 1
    old   2: WHERE &val >= (SELECT COUNT(*) FROM emp e2
    new   2: WHERE 1 >= (SELECT COUNT(*) FROM emp e2
    ENAME                                      SAL
    KING                                      5000Using in-line view and rownum:
    SQL> ed
    Wrote file afiedt.buf
      1  SELECT * FROM (SELECT ename,sal FROM emp ORDER BY SAL desc)
      2* WHERE rownum<2
    SQL> /
    ENAME                                      SAL
    KING                                      5000Using analytical function ROW_NUMBER
    SQL> ed
    Wrote file afiedt.buf
      1  SELECT * FROM (SELECT ename,sal,ROW_NUMBER() OVER(ORDER BY sal desc) rn FROM emp )
      2* WHERE rn=1
    SQL> /
    ENAME                                      SAL         RN
    KING                                      5000          1Also you can use several other analytical functions as mentioned by Kartick. Search and read on the internet to find what is most suitable way and why!.
    http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:2853107469873
    http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:12759079666984

  • Oracle 9i SQL: Find nth highest

    Hi,
    Please help me to understand the logic behind the following query to find the nth highest:
    select empNo, salary
    from emp a
    where (n-1) = (select count(*) from (select distinct salary from emp)b where b.salary > a.salary )
    Thanks,
    priya ranjan

    Read the following marvellous link:
    http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html
    Greetings,
    Simon

  • How to retrieve 5th highest salary in a single statement

    Hi,
    How can we retrieve 5th highest salary in a single statement

    Another method
    select top 1
     OrderID
    , Quantity
    from
     select top 5
      OrderID
     , Quantity = sum (Quantity)
     from
      [Order Details]
     group by
      OrderID
     order by
      2 desc
    ) q
    order by
     2  asc
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • How to get highest salary from salary column in sharepoint list

    Hello,
    I have one custom list in which there is one salary column so I want to get the highest salary.Can we do this OOTB or by using custom code by adding webpart(using CAML query).
    Thanks,

    http://stackoverflow.com/questions/516073/max-query-using-caml
    <Query>
    <OrderBy>
    <FieldRef Name="particularcolumn" Ascending="FALSE" />
    </OrderBy>
    </Query>
    http://stackoverflow.com/questions/8383616/caml-query-on-a-sharepoint-2010-list
    http://sharepoint.stackexchange.com/questions/16955/how-to-select-max-field-in-caml-query
    If this helped you resolve your issue, please mark it Answered

  • ABAP Query to display top 5 employees who are having highest salary

    How will you write a query in abap to display top 5 emplyeee records who are having Highest Salary Among all the employees.
    Please Reply...

    Hi Kush,
    Before writing any Database Query, always keep 2 important thumb rules in your mind :
    1. Keep the number of data base access small
    2. Keep the amount of data transfer small.
    As you mentioned the Database Table has only 100 records, you can locally buffer the Table. So, make a local buffering and then you can sort the table based on the salary.
    Select *
      from Table_name
    into table gt_itab.
    Sort gt_itab by salary descending.
    Now you can read the  entries one after the another based on the index.
    Have a look at the following code for an efficient performance feature. Goto SE38 and have a look at the ABAP Examples.
    demo_select_some_columns. Package : SABAPDEMOS
    Hope this will help.
    Thanks,
    Samantak.

  • To find 2nd highest salary in employee table

    Hi,
    I want to know how to calculate the 2nd highest salaried employee details.
    rownum "=" sign cannot be used i suppose and
    with and without using orderby clause.
    both the methods needed.
    cheers,

    With order by
    select * from
       ( select q.*, rownum as rn from
          ( select * from emp order by sal desc ) q
    where rn=2
    /With analytics:
    select empno , sal
    from ( select empno , sal
                  , dense_rank () over (order by sal desc) as sal_rank
           from emp )
    where sal_rank = 2Notice that DENSE_RANK() might give us a different result compared to RANK() and both give us a different result from the first example.
    Cheers, APC

  • SQL query on emp table(SCOTT Schema): Highest salary per department

    From emp table of SCOTT schema. I would like to know the name, sal,the deptno he belongs of the highest paid employee from each dept.
    Explaining further:
    The following query only returns deptno and max(sal) of each department. I need the name of the highest paid employee from each dept along with the below result.
    SQL> SELECT DEPTNO,MAX(SAL) FROM EMP GROUP BY deptno;
    DEPTNO MAX(SAL)
    30 2850
    20 3000
    10 5000

    It seems you want just one ename if two employees have the same highest salary. In that case you can use this construct:
    SQL> select deptno
      2       , max(sal)
      3       , max(ename) keep (dense_rank last order by sal)
      4    from emp
      5   group by deptno
      6  /
        DEPTNO   MAX(SAL) MAX(ENAME)
            10       5000 KING
            20       3000 SCOTT
            30       2850 BLAKE
    3 rijen zijn geselecteerd.Regards,
    Rob.

  • How to find the third highest salary by distinct the salary from deptno20,from employee table

    how to find the third highest salary by distinct the salary from deptno20,by using employee table

    You already asked this question, a half hour earlier:
    https://forums.oracle.com/thread/2569985
    and received a reply.
    Do not multi-post to the forums.
    These are user-to-user forums and everyone is posting because they volunteer to post and not because they get paid to post.    Everyone else is paid exactly the same as you are paid to post here.   When you multi-post is makes you appear as impatient and expecting instantaneous answers.
    That is poor forum etiquette, approaching behavior similar to spamming the forums.
    This duplicate post is locked.

  • How to find the third highest salary from deptno20,from employee table

    how to find the third highest salary from deptno20,from employee table

    SELECT *
      FROM emp;
    EMPNO
    ENAME
    JOB
    MGR
    HIREDATE
    SAL
    COMM
    DEPTNO
    7369
    SMITH
    CLERK
    7902
    12/17/1980
    1000
    3
    20
    7499
    FEDERAL
    SALESMAN
    7654
    2/20/1981
    2000
    4
    30
    7521
    WARD
    SALESMAN
    7698
    2/22/1981
    3000
    4
    30
    7566
    JONES
    MANAGER
    7839
    4/2/1981
    4000
    3
    20
    7839
    MARTIN
    SALESMAN
    7698
    9/28/1981
    5421
    4
    30
    7698
    BLAKE
    MANAGER
    7698
    5/1/1981
    6222
    4
    30
    7782
    CLARK
    MANAGER
    7839
    6/9/1981
    5222
    2
    10
    7788
    SCOTT
    ANALYST
    7566
    12/9/1982
    5463
    3
    20
    7839
    KING
    PRESIDENT
    7902
    11/17/1981
    8543
    2
    10
    7844
    TURNER
    SALESMAN
    7698
    9/8/1981
    2124
    4
    30
    7876
    ADAMS
    CLERK
    7788
    1/12/1983
    2125
    3
    20
    7900
    JAMES
    CLERK
    7698
    12/3/1981
    5462
    4
    30
    7902
    FORD
    ANALYST
    7566
    12/3/1981
    2132
    3
    20
    7934
    MILLER
    CLERK
    7782
    1/23/1982
    5165
    2
    10
    SELECT sal, deptno
      FROM (SELECT   a.*,
                     DENSE_RANK () OVER (PARTITION BY deptno ORDER BY sal DESC) r
                FROM emp a
          ----  ORDER BY sal DESC
    WHERE r = 3;
    SAL
    DEPTNO
    5421
    30
    5165
    10
    2132
    20
    Regards ,
    Friend
    Message was edited by: MostWanted!!!!

  • 2nd and 5th highest salary

    hi, i am kind of new to sql. i have a table with employee name and salary.
    i would like to find the name of the 2nd highest salary? also i like to get the name of the 5th highest salary
    here is my data
    INSERT INTO er4 VALUES('john', 1000, 123);
    INSERT INTO er4 VALUES('john2', 3000, 234);
    INSERT INTO er4 VALUES('john3', 1000, 223);
    INSERT INTO er4 VALUES('john4', 4000, 123);
    INSERT INTO er4 VALUES('john5', 8000, 023);
    INSERT INTO er4 VALUES('john6', 9000, 023);
    INSERT INTO er4 VALUES('john7', 8000, 723);
    you can see in the data that there are two employee with highest second salary. i would like to get either one but i want my query to return one row
    can somebody help me write a query to get the name and salary of 2nd highest and name and salary of 5th highest

    Hi,
    Part of learning about functions is learning what they are good for, and what they are not good for.
    As Justin said, MAX is not good for this task.
    If you were interested in just the 1st highest salary, you possibly could use MAX like this:
    SELECT  *
    FROM    er4
    WHERE   salary  =      (
                   SELECT     MAX (salary)
                   FROM     er4
    AND     ROWNUM     = 1     -- in case of a tie, pick one row arbitrarily
    ;But even if the goal was to find the highest salary, I would use ROW_NUMBER, like Justin did.
    Why? The following scenario happens all the time in real life. The client for whom you wrote this comes back in 3 months and says: "That query you wrote for the highest salary works great! We've found it so useful, we'd like to do something similar to find the 2nd highest salary, or the 5th highest, or the top 5, or the next 5." ROW_NUMBER is flexible enough to do all these things. When you have to solve one problem, you'll save time (in the long run) if you can foresee, or just guess, what similar problems you might need to solve later, and write something for today's problem that can easily be adapted for tomorrow's potential problem.

Maybe you are looking for