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

Similar Messages

  • Query to find Employee Salary Details

    Hi,
    Could anyone help in writing the query to find employee salary details.
    Thanks in advance.

    This should get you started:
    SELECT papf.full_name
    ,papf.email_address
    ,ppp.proposed_salary_n salary
    FROM per_pay_proposals ppp
    ,per_all_assignments_f paaf
    ,per_all_people_f papf
    WHERE ppp.assignment_id = paaf.assignment_id
    AND paaf.assignment_type = 'E'
    AND paaf.primary_flag = 'Y'
    AND paaf.person_id = papf.person_id
    AND nvl(papf.current_employee_flag, 'N') = 'Y'
    AND trunc(sysdate) BETWEEN
    ppp.change_date AND ppp.date_to
    AND trunc(sysdate) BETWEEN
    paaf.effective_start_date AND paaf.effective_end_date
    AND trunc(sysdate) BETWEEN
    papf.effective_start_date AND papf.effective_end_date;

  • 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).

  • Query to find the Salary table details  HRMS 11i

    I am looking  query to find the persons salary details in Oralce EBS 11i.
    I tried the below query bu it didn'ty work.
    SELECT papf.employee_number
    ,papf.full_name
    ,pj.NAME job
    ,haou.NAME ORGANIZATION
    ,ppp.proposed_salary_n salary
    FROM per_all_people_f papf
    ,per_all_assignments_f paaf
    ,per_jobs pj
    ,hr_all_organization_units haou
    ,per_position_definitions ppd
    ,per_all_positions pap
    ,per_pay_proposals ppp
    WHERE 1 = 1
    AND SYSDATE BETWEEN papf.effective_start_date AND papf.effective_end_date
    AND papf.current_employee_flag = 'Y'
    AND papf.employee_number IS NOT NULL
    AND paaf.person_id = papf.person_id
    AND SYSDATE BETWEEN paaf.effective_start_date AND paaf.effective_end_date
    AND paaf.job_id = pj.job_id
    AND paaf.organization_id = haou.organization_id
    AND paaf.position_id = pap.position_id
    AND pap.position_definition_id = ppd.position_definition_id
    AND ppp.pay_proposal_id = (SELECT MAX (pay_proposal_id)
    FROM per_pay_proposals
    WHERE assignment_id = paaf.assignment_id)
    In our case all the below tables have 0 records..
    select count(*) from PER_PAY_PROPOSALS ;
      COUNT(*)
             0
    select count(*) from pay_element_entry_values_f;
      COUNT(*)
             0
    select count(*) from PAY_ELEMENT_ENTRY_VALUES_F;
      COUNT(*)
             0
    select count(*) from PAY_ELEMENT_ENTRIES_F;
      COUNT(*)
             0

    Hi,
    Your results clearly states that there is no salary data which is been captured as well as there are no element entries which are assigned to any assignment.
    Please do the below which will insert records in salary as well as element entries table:
    1. Add a salary proposal to any active employee (People Enter and Maination --> Search for any employee --> Assignment --> Salary) - This will insert a record in PER_PAY_PROPOSALS table
    2. Add an element entry to any active assignment (People Enter and Maination --> Search for any employee --> Assignment --> Entries) - This will insert a record in PAY_ELEMENT_ENTRIES_F and PAY_ELEMENT_ENTRY_VALUES_F table
    Hope this clarifies.
    Thanks,
    Sanjay

  • Query to find basic salary

    Hi ,
    I need to find basic salary of all the employees from their actual salary
    10% of his basic salary will be debited at the end of the month 
    If the employee is about to relieve the company ,he should be paid by 5% extra bonus from his final settlement
    How to write a query for the above scenario

    Hello,
    PFB structure and expected result
    Name  Location  Salary
    smith newyork  
    6000
    adam  dallas   
    5000
    john  delhi    
    7000
    scott laondon  
    4000
    Name  Location  Salary  Basic salary Monthly_Debit Extra Bonus
    ================================================================
    smith newyork   6000      250              25                12.5    
    adam  dallas    5000      208              20.8             10.4    
    John  delhi     7000      291              29.1             14.55
    scott laondon   4000      166              16.6             8.3

  • Query to find employee salary details in HRMS

    Hi,
    Could anyone one provide me the query to get the salary details of an employee.
    Thanks.

    Hi,
    Hope this work for you.
    SELECT pap.last_name last_name
    ,pap.first_name first_name
    ,pap.employee_number employee_id
    ,hlu2.meaning ethnic_origin
    ,hlu1.meaning eeo_category
    ,hlu5.meaning veteran_status
    ,pap.sex sex
    ,ROUND(pro.proposed_salary_n, 2) current_salary
    ,ROUND(pro.proposed_salary_n * ppb.pay_annualization_factor) current_yearly_salary
    ,pg.NAME grade
    ,pj.NAME job_name
    ,pjd.segment3 business_unit
    ,pjd.segment4 FUNCTION
    ,pjd.segment5 designation
    ,pjd.segment2 job_group
    ,pos.date_start hire_date
    ,NVL(pos.actual_termination_date, hr_general.end_of_time) termination_date
    ,hl.location_code LOCATION
    ,pap.full_name
    ,pj.job_information3 flsa_code
    ,hlu4.meaning employment_category_meaning
    ,pap.effective_start_date p_effective_start_date
    ,pap.effective_end_date p_effective_end_date
    ,paa.effective_start_date a_effective_start_date
    ,paa.effective_end_date a_effective_end_date
    ,pro.from_change_date s_effective_from_date
    ,NVL(pro.to_change_date, hr_general.end_of_time) s_effective_to_date
    ,pap.current_employee_flag
    FROM per_all_people_f pap
    ,per_all_assignments_f paa
    ,per_periods_of_service pos
    ,(SELECT pro1.approved
    ,pro1.assignment_id
    ,pro1.change_date from_change_date
    , pro_next.change_date - 1 to_change_date
    ,pro1.multiple_components
    ,pro1.last_change_date
    ,pro1.proposed_salary_n
    ,pro1.forced_ranking
    ,pro1.last_updated_by
    ,pro1.last_update_date
    ,pro1.event_id
    ,pro1.performance_review_id
    ,pro1.pay_proposal_id
    FROM per_pay_proposals pro1
    ,per_pay_proposals pro_next
    WHERE pro1.assignment_id = pro_next.assignment_id(+)
    AND pro1.change_date = pro_next.last_change_date(+)) pro
    ,per_jobs pj
    ,per_job_definitions pjd
    ,hr_locations hl
    ,per_grades pg
    ,(SELECT *
    FROM hr_lookups
    WHERE lookup_type = 'US_EEO1_JOB_CATEGORIES') hlu1
    ,(SELECT *
    FROM hr_lookups
    WHERE lookup_type = 'US_ETHNIC_GROUP') hlu2
    ,(SELECT *
    FROM hr_lookups
    WHERE lookup_type = 'US_EXEMPT_NON_EXEMPT') hlu3
    ,(SELECT *
    FROM hr_lookups
    WHERE lookup_type = 'EMP_CAT') hlu4
    ,(SELECT meaning
    ,lookup_code
    FROM hr_lookups
    WHERE lookup_type LIKE 'US_VETERAN_STATUS') hlu5
    ,per_pay_bases ppb
    ,(SELECT *
    FROM per_addresses
    WHERE primary_flag = 'Y'
    AND address_type = 'H'
    AND ( date_to IS NULL
    OR date_to > SYSDATE)) addr
    WHERE pap.person_id = paa.person_id
    AND paa.assignment_id = pro.assignment_id
    AND paa.job_id = pj.job_id
    AND paa.location_id = hl.location_id
    AND paa.grade_id = pg.grade_id(+)
    AND pos.person_id = pap.person_id
    AND ppb.pay_basis_id = paa.pay_basis_id
    AND pj.job_information1 = hlu1.lookup_code
    AND pj.job_definition_id = pjd.job_definition_id
    AND pap.per_information1 = hlu2.lookup_code(+)
    AND pap.per_information5 = hlu5.lookup_code(+)
    AND pj.job_information3 = hlu3.lookup_code(+)
    AND paa.employment_category = hlu4.lookup_code
    AND addr.person_id(+) = pap.person_id

  • 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.

  • 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.

  • Query to find employee annual salary or pay rate for a pay period (Bi-Week)

    Hi Guru's,
    need your help for finding annual salary or pay rate for pay period for an employee.
    I have below query returning some thousand line which I cant understand. Can someone correct the query please.
    SELECT ppp.proposed_salary_n salary From per_pay_proposals ppp,apps.per_all_people_f papf,apps.per_all_assignments_f paaf,apps.per_all_people_f papf1 WHERE papf.person_id = paaf.person_id AND paaf.primary_flag = 'Y' AND paaf.assignment_type = 'E' AND paaf.supervisor_id = papf1.person_id AND papf1.current_employee_flag = 'Y'AND papf.business_group_id = paaf.business_group_id AND SYSDATE BETWEEN papf.effective_start_date and papf.effective_end_date AND SYSDATE BETWEEN paaf.effective_start_date AND paaf.effective_end_date AND SYSDATE BETWEEN papf1.effective_start_date AND papf1.effective_end_date AND papf.employee_number='1234';
    SALARY
    17.4346
    16.0846
    17.4346
    13.78
    13.78
    15.07
    13.78
    13.78
    13.78
    3305.59
    14.859
    SALARY
    5507.25
    2731.01
    2690.51
    13.78
    13.35
    13.35
    1960
    4192
         17
    4927
    2525.02
    SALARY
    2652
    13.35
    15.07
    2686.67
    2964
    13.78
    17.4635
    20.4981
    16.0846
    13.78
    17.4635
    SALARY
    2666.68
    13.78
    15.07
    13.78
    16.0846
    17.4635
    4353.99
    4562.51
    17.4346
    16.0846
    6727.41
    SALARY
    2780.99
         20
    17.4346
    16.0846
    2970
    2315.01
    17.4635
    2629.85
    14.5705
    5635
    17.4346
    Thanks
    Sandeep

    I used table hrpy_rgdir  to get sequence number for the given pernr, payroll area and dates.
    Using the sequence number, I am calling Function PYXX_READ_PAYROLL_RESULT to extract the payroll results.
    SELECT * INTO CORRESPONDING FIELDS OF TABLE it_hrpy_rgdir
      FROM hrpy_rgdir
      WHERE srtza = 'A'             
        AND paydt IN sopaydt
        AND abkrs IN soabkrs
        AND pernr IN sopernr.
    LOOP AT it_hrpy_rgdir.
      PERFORM get_rt USING it_hrpy_rgdir-pernr it_hrpy_rgdir-seqnr.
    ENDLOOP.
    *&      Form  get_rt
    *       text
    *      -->PERNR      text
    *      -->SEQNR      text
    FORM get_rt USING pernr seqnr.
      CLEAR: it_payroll_result.
      CALL FUNCTION 'PYXX_READ_PAYROLL_RESULT'
        EXPORTING
          clusterid                    = 'RQ'
          employeenumber               = pernr
          sequencenumber               = seqnr
        CHANGING
          payroll_result               = it_payroll_result
        EXCEPTIONS
          illegal_isocode_or_clusterid = 1
          error_generating_import      = 2
          import_mismatch_error        = 3
          subpool_dir_full             = 4
          no_read_authority            = 5
          no_record_found              = 6
          versions_do_not_match        = 7
          error_reading_archive        = 8
          error_reading_relid          = 9
          OTHERS                       = 10.
    ENDFORM.                   
    For any period, the function module just returns sy-subrc = 6 [no_record_found].
    We are on Australian Payroll.  Could you please let me know what I might be missing or is there an alternative way of getting the information ?
    Thank you

  • 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!!!!

  • How to find record having second highest salary

    Hello friends
    I ve a table empsal
    it has 2 columns name,salary
    i need to find out name of employee with second highest salary.
    how shud i ..pls help...
    thks
    sonal...

    in the doc
    create or replace type SecondMaxImpl as object
      max NUMBER, -- highest value seen so far
      secmax NUMBER, -- second highest value seen so far
      static function ODCIAggregateInitialize(sctx IN OUT SecondMaxImpl)
        return number,
      member function ODCIAggregateIterate(self IN OUT SecondMaxImpl,
        value IN number) return number,
      member function ODCIAggregateTerminate(self IN SecondMaxImpl,
        returnValue OUT number, flags IN number) return number,
      member function ODCIAggregateMerge(self IN OUT SecondMaxImpl,
        ctx2 IN SecondMaxImpl) return number
    create or replace type body SecondMaxImpl is
    static function ODCIAggregateInitialize(sctx IN OUT SecondMaxImpl)
    return number is
    begin
      sctx := SecondMaxImpl(0, 0);
      return ODCIConst.Success;
    end;
    member function ODCIAggregateIterate(self IN OUT SecondMaxImpl, value IN number) return number is
    begin
      if value > self.max then
        self.secmax := self.max;
        self.max := value;
      elsif value > self.secmax then
        self.secmax := value;
      end if;
      return ODCIConst.Success;
    end;
    member function ODCIAggregateTerminate(self IN SecondMaxImpl,
        returnValue OUT number, flags IN number) return number is
    begin
      returnValue := self.secmax;
      return ODCIConst.Success;
    end;
    member function ODCIAggregateMerge(self IN OUT SecondMaxImpl, ctx2 IN SecondMaxImpl) return number is
    begin
      if ctx2.max > self.max then
        if ctx2.secmax > self.secmax then
          self.secmax := ctx2.secmax;
        else
          self.secmax := self.max;
        end if;
        self.max := ctx2.max;
      elsif ctx2.max > self.secmax then
        self.secmax := ctx2.max;
      end if;
      return ODCIConst.Success;
    end;
    end;
    CREATE OR REPLACE FUNCTION SecondMax (input NUMBER) RETURN NUMBER
    PARALLEL_ENABLE AGGREGATE USING SecondMaxImpl;
    SELECT deptno,SecondMax(sal)
    FROM emp
    GROUP BY deptno;
        DEPTNO SECONDMAX(SAL)
            10           2450
            20           3000
            30           1600

  • 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

  • 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>

Maybe you are looking for

  • 5530 firmware update found but not compatible?

    Hi,  the phone is a french 5530 (no fimlock, no netlock, a free phone), a firmware update is found from nokia, i can download it from the phone. Then it restarts the phone, and a white screen appears with the update bar. But then it just restarts and

  • Spaces and Call of Duty 2?

    Hey everyone. I noticed that when I open Call of Duty 2, I can't use spaces. The COD2 application takes up the whole screen and doesn't allow me to use expose, spaces, etc. Is there anyway to make spaces available while I'm using COD2 so that I can e

  • Mac keeps restarting before installing windows

    I used bootcamp to make a partition of 200gb for windows, when i clicked install in bootcamp my mac restarted and started installing windows xp. When it is finished it again reboots. Than i cant choose between my windows or my mac partition and the w

  • Nano Video Update ????

    Has anyone heard or seen any info for any updates to allow the Nano to play Video...

  • Where has my Itunes content gone?

    I opened itunes today and many things were gone i.e. all apps and podcasts, and all of my playlists have gone.  when I plugged in my iphone to itunes it also showed no apps ,podcasts music. what could be wrong?