Query to find the nth highest salary...

Hi guys,.

I can't understand this query that we got a Web site. It is used to find the nth highest salary of the employee... Can someone explain to me please each and every part of it...

The Charly is:

Select distinct (a.salary)
EMP a
where 1 = (select count (distinct (salary))
b emp
where a.salary < = b.salary)

The "BOLD" part which is the return value... is my main question about this query...

Help, please

It is called a correlated sub query. The inner query is executed for each iteration of the outer query. For example, if the table emp has 14 rows and then for each salary 14 the inner query will run.
The part

select count(distinct(sal))
from emp b
where a.sal<=b.sal

actually counts the number of treatments that are less than or equal to the treatment of the outer query.
Clearly for the King, who gets the highest salary is greater than or equal to 1 (himself). It returns the highest salary.

SQL> select *
  2  from emp a
  3  where 1 >= (select count(distinct(sal))
  4  from emp b
  5  where a.sal<=b.sal)
  6  order by sal desc;

     EMPNO ENAME                               JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ----------------------------------- --------- ---------- --------- ---------- ---------- ----------
      7839 KING                                PRESIDENT            17-NOV-81       5000                    10

Salary of the first two.

SQL> ed
Wrote file afiedt.buf

  1  select *
  2  from emp a
  3  where 2 >= (select count(distinct(sal))
  4  from emp b
  5  where a.sal<=b.sal)
  6* order by sal desc
SQL> /

     EMPNO ENAME                               JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ----------------------------------- --------- ---------- --------- ---------- ---------- ----------
      7839 KING                                PRESIDENT            17-NOV-81       5000                    10
      7902 FORD                                ANALYST         7566 03-DEC-81       3000                    20
      7788 SCOTT                               ANALYST         7566 09-DEC-82       3000                    20

Tags: Database

Similar Questions

  • find the nth highest salary

    Hi guys, I'll try to find the nth highest salary

    using the sub query, is there a case in which the following query may fail

    Select

    * de (

    Select

    distinct rownum rn,salary from emp_mgr order by rownum) t where t. rn = 3

    ;

    I do something like that, but he got 3 selects, so I'm a worried buit, trying to optimize it

    Select * from)

    Select rownum rn, t t.salary (salary select distinct from emp_mgr by salary desc)

    ) rs

    where rs.rn = &n;

  • query to retrieve the second highest salary managers

    Select * from employee where salary = (select max (salary) in e employees where employee_id = e.manager_id and salary < (select max (salary) in employees))
    /

    It does not run... can someone suggest a new

    987184 wrote:
    our teacher was asked to write without y analytical function.dats.

    1 list of Manager IDs:

    select  manager_id
      from  hr.employees
      where manager_id is not null
    /
    

    2 manager info:

    select  employee_id,
            first_name,
            last_name,
            salary
      from  hr.employees
      where employee_id in (
                            select  manager_id
                              from  hr.employees
                              where manager_id is not null
                           )
    /
    

    3. Manager second highest salary

    with managers as (
                      select  employee_id,
                              first_name,
                              last_name,
                              salary
                        from  hr.employees
                        where employee_id in (
                                              select  manager_id
                                                from  hr.employees
                                                where manager_id is not null
                                             )
                     )
    select  m1.employee_id,
            m1.first_name,
            m1.last_name,
            max(m1.salary) second_highest_salary
      from  managers m1,
            managers m2
      where m1.salary < m2.salary
      group by m1.employee_id,
               m1.first_name,
               m1.last_name
      having count(distinct m2.salary) = 1
    /
    

    And execution:

    SQL> select  employee_id,
      2          first_name,
      3          last_name,
      4          salary
      5    from  hr.employees
      6    where employee_id in (
      7                          select  manager_id
      8                            from  hr.employees
      9                            where manager_id is not null
     10                         )
     11    order by salary desc
     12  /
    
    EMPLOYEE_ID FIRST_NAME           LAST_NAME                     SALARY
    ----------- -------------------- ------------------------- ----------
            100 Steven               King                           24000
            102 Lex                  De Haan                        17000
            101 Neena                Kochhar                        17000
            145 John                 Russell                        14000
            146 Karen                Partners                       13500
            201 Michael              Hartstein                      13000
            108 Nancy                Greenberg                      12008
            205 Shelley              Higgins                        12008
            147 Alberto              Errazuriz                      12000
            114 Den                  Raphaely                       11000
            148 Gerald               Cambrault                      11000
    
    EMPLOYEE_ID FIRST_NAME           LAST_NAME                     SALARY
    ----------- -------------------- ------------------------- ----------
            149 Eleni                Zlotkey                        10500
            103 Alexander            Hunold                          9000
            121 Adam                 Fripp                           8200
            120 Matthew              Weiss                           8000
            122 Payam                Kaufling                        7900
            123 Shanta               Vollman                         6500
            124 Kevin                Mourgos                         5800
    
    18 rows selected.
    
    SQL> with managers as (
      2                    select  employee_id,
      3                            first_name,
      4                            last_name,
      5                            salary
      6                      from  hr.employees
      7                      where employee_id in (
      8                                            select  manager_id
      9                                              from  hr.employees
     10                                              where manager_id is not null
     11                                           )
     12                   )
     13  select  m1.employee_id,
     14          m1.first_name,
     15          m1.last_name,
     16          max(m1.salary) second_highest_salary
     17    from  managers m1,
     18          managers m2
     19    where m1.salary < m2.salary
     20    group by m1.employee_id,
     21             m1.first_name,
     22             m1.last_name
     23    having count(distinct m2.salary) = 1
     24  /
    
    EMPLOYEE_ID FIRST_NAME           LAST_NAME                 SECOND_HIGHEST_SALARY
    ----------- -------------------- ------------------------- ---------------------
            101 Neena                Kochhar                                   17000
            102 Lex                  De Haan                                   17000
    
    SQL> 
    

    As you can see, most high salary 24000 is won by the King. Second highest salary is 17000 and is won by two managers: Kochhar and De Haan.

    SY.

  • Query to find the coordinates of employee salary

    Hello

    Could someone help write the query to find the salary of the employee details.

    Thanks in advance.

    This should help you get started:

    SELECT papf.full_name
    papf.email_address
    ppp.proposed_salary_n salary
    OF per_pay_proposals ppp
    per_all_assignments_f ADP
    per_all_people_f women's wear
    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;

  • To find the nth higher/lower value in a table

    Hi all

    I have a question

    in the table a1(empid,sal) are the 2 columns

    EmpID sal

    100-5000

    105 7500

    110 3500

    087 11136

    14336 95

    I didn't want to write the query that will give me the 3rd highest salary. or generalized to find the nth more eleve/nieme the lowest value of the table.

    Note:-Rownum and Rowid must not be used

    Thank you

    Hello

    You can use a generic result of queries to find nth highest salary...

    Just put the value of N in the query, you will get the answer...

    Query:

    SELECT E1. Salary OF EMP E1 WHERE (N) = (SELECT COUNT (DISTINCT E2. Salary) OF EMP E2 WHERE E2. Salary > = E1. Salary);

    hope you got the answer...

    Check if it really helps...

    thanxx...

  • SQL query to find the total number of source based nonsource passangersbetween source and destination station and passenger station on the same chekindate

    Hello

    SQL query to find the total number of source based nonsource passangersbetween source and destination station and passenger station on the same chekindate.

    Please help on this script and let me know if you need more details.

    ---

    You use a SELECT statement.  Let me know if you need more details.

  • What is the query to find the name of all applications for all EBS R12.1.3 modules?

    What is the query to find the name of all applications for all EBS R12.1.3 modules?

    With regard to:

    Mr. Shahzad Saleem

    Try:

    SELECT * FROM fnd_concurrent_programs_vl;

  • Query to find the Pages and the list of permissions associated with a particular role in PS

    I want a query to find the Pages and the list of permissions associated with specific roles in PS.

    For example, if we see the role of manager accounts payable, it conatins Pages and the list of permissions.

    But, to get everything in EXCEL sheet by Manuel priocess's BIG job. So, can someone give me the query.

    Please try under queries

    The roles assigned to the list of Perm:

    SELECT B.ROLENAME

    OF PSCLASSDEFN A, PSROLECLASS B

    WHERE (A.CLASSID = B.CLASSID

    AND A.CLASSID =: 1).

    List of Perm pages can access:

    SELECT B.MENUNAME, B.BARNAME, B.BARITEMNAME, B.PNLITEMNAME, C.PAGEACCESSDESCR, B.DISPLAYONLY

    OF PSCLASSDEFN A, PSAUTHITEM B, PSPGEACCESSDESC C

    WHERE (A.CLASSID = B.CLASSID

    AND A.CLASSID =: 1

    AND B.BARITEMNAME > ' '

    AND B.AUTHORIZEDACTIONS = C.AUTHORIZEDACTIONS)

  • query to find the dependent task, attached to the task on a response in OIM 11 g

    can someone help me to do a sql query to find the dependent task, attached to the task on a response in OIM 11 g

    Published by: user13331347 on Sep 3, 2012 14:09

    Use under query to find the dependent task in OIM 11 g: -.

    Select pkg.pkg_name, mil.mil_name, rsc.rsc_data, rsc.sta_key, sta.sta_status, sta.sta_bucket, mil2.mil_name
    pkg pkg, tos tos, mil mil, mil mil2, rsc rsc, sta sta, rgm the rgm
    where pkg.pkg_key = tos.pkg_key
    and tos.tos_key = mil.tos_key
    and mil.mil_key = rsc.mil_key
    and rsc.sta_key = sta.sta_key
    and rgm.rsc_key = rsc.rsc_key
    and rgm.mil_key = mil2.mil_key
    order of pkg.pkg_name, mil.mil_name, rsc.rsc_data, sta.sta_status, mil2.mil_name

  • Query to find the name of the partition

    Hi all

    I'm a little surprised to see that it is not a column I can use to query the name of the partition for a table, in the case of partition of Internval per day...

    I'm looking for a simple query find the name of the partition for the word partition of today ' today... Am I missing something?

    Ask something like...

    Select nom_partition from dba_Tab_partitions where table_name = 'XYZ' and TO_DATE(HIGH_VALUE,'DD-MON-YYYY') = April 2, 2014"

    High value being a long column, lets you convert CHAR or DATE?

    Thanks in advance

    Then, you create a copy of dba_tab_partitions, since you can only convert the LONG data during an insertion type or create the table.

    From Dan article: «just...» If you have a LONG, and you want to appear more than it, you must use TO_LOB to convert it during either an INSERT INTO

    or a CREATE TABLE As . Everything else is just wasting time. "

  • to find the lowest a salary table

    Hello guys,.
    I need to find the salary low table. What is the easy way to do it.

    How do you manage the links?

    If you want several rows returned when there are several people with the same salary

    SELECT *
      FROM(
        SELECT a.*,
               rank() over (order by salary desc ) rnk
          FROM your_table a)
     WHERE rnk = 1
    

    If you want to handle differently the links, use a different analytic function. No doubt dense_rank or row_number.

    Justin

  • Query to find the previous activity in STM

    I have a data base to reflect a State of Transition (STM) Machine. It contains 3 tables: activity, node, and Transition.

    Tables of knots and Transition are the configuration of the STM. They define the different possible States and the possible transitions between States.

    The way that I take through the STM is recorded in the activity Table. A record is created for each visited node.

    I need a query that uses the id of my current activity to find my previous activity, i.e. the activity that precedes me directly. The following rules apply:
    -My Id is meaningless
    -It should work for different configurations of the transition and the node tables, but:
    -The starting node is always the node "A".
    -There is no recursive transition, for example ('B', 'B');
    -There is no circular transitions, for example ('B', 'C') and ('C', 'B')
    -Transition and node tables will never change between the creation of the first activity and the execution of the query.
    -The path reflected in the activity table is always a valid path.
    -My current activity is always the last activity.

    For all data below, find the previous activity of activity with id = 4. The correct answer is "C".
    DROP TABLE Transition;
    DROP TABLE Activity;
    DROP TABLE Node;
    
    CREATE TABLE Node
    (
         Id VARCHAR2(1) PRIMARY KEY
    );
    
    CREATE TABLE Activity
    (
         Id Number(8,0) PRIMARY KEY,
         Node_Id VARCHAR2(1)
    );
    
    CREATE TABLE Transition
    (
         FromNode_Id VARCHAR2(1),
         ToNode_Id VARCHAR2(1)
    );
    
    ALTER TABLE Activity
    ADD FOREIGN KEY
    (Node_Id) REFERENCES Node(Id);
    
    ALTER TABLE Transition
    ADD FOREIGN KEY
    (FromNode_Id) REFERENCES Node(Id);
    
    ALTER TABLE Transition
    ADD FOREIGN KEY
    (ToNode_Id) REFERENCES Node(Id);
    
    INSERT INTO Node VALUES ('A');
    INSERT INTO Node VALUES ('B');
    INSERT INTO Node VALUES ('C');
    INSERT INTO Node VALUES ('D');
    
    INSERT INTO Transition VALUES ('A','B');
    INSERT INTO Transition VALUES ('B','C');
    INSERT INTO Transition VALUES ('B','D');
    INSERT INTO Transition VALUES ('C','D');
    
    INSERT INTO Activity VALUES (1,'A');
    INSERT INTO Activity VALUES (2,'B');
    INSERT INTO Activity VALUES (3,'C');
    INSERT INTO Activity VALUES (4,'D');
    Desired output:
    ID
    -
    C

    Hello

    Assuming that all Activity_id tells us nothing on the way, but all lines of activity together to form a single path, we can get results this way:

    WITH     all_paths    AS
    (
         SELECT     'A' || SYS_CONNECT_BY_PATH (a.Node_id, '/')
                  || '/'     AS node_id_path
         FROM     Transition  t
         JOIN     Activity    a  ON  a.Node_Id  = t.ToNode_Id
         WHERE     LEVEL             = (
                                SELECT  COUNT (*)
                             FROM     activity
                             ) - 1
         START WITH  t.FromNode_Id  = 'A'
         CONNECT BY  t.FromNode_Id  = PRIOR t.ToNode_Id
    )
    SELECT       REGEXP_SUBSTR ( node_id_path
                     , '([^/]+)/' || (
                                        SELECT  Node_Id
                                 FROM    activity
                                 WHERE   Id     = 4  -- target_id
                                    )
                             || '/'
                   , 1
                   , 1
                   , NULL
                   , 1
                   )     AS prev_id
    FROM       all_paths
    ;
    

    This means, there is some character (I used "/" above) that we know never appears in Activity.Node_Id.

    Since there are no loops in Transition, it cannot be more than 1 way that involves all activity lines. If there are N lines in operation, this full path will be one that extends to LEVEL = N - 1. (It is not N, N - 1, because the join between the activity and the Transition will always leave 1 line in activity with Node_Id = 'A'). As soon as we have the full path (which is node_id_path in 1 row, produced by all_paths), we just need to guess what was the Node_Id, which corresponds to the id of the target (4 in this example) and find the previous of the delimited node_id_path node_id.

  • Query to find the sales order

    Hi all
    I need a query to find sales order number against which a serial number is processed.

    Can ny helps an onthis.

    Thank you

    Hello
    Try the following question

    Select wdd.source_document_number
    Of wsn wsh_serial_NUMBERS,.
    wsh_delivery_details JMD
    Where wsn.delivery_detail_id = wdd.delivery_detail_id
    And & wsn.fm_serial_number series
    and nvl (wsn.ti_serial_number, wsn.fm_serial_number)

    Thank you
    Claire

  • Query to find the setting and add it to the minimum value

    Hi, I need a query to run under operation
    I have a table as shown below

    chg tax code Bill rec_no r_cost d_cost
    12345 1 114,35 100.30 1.00 1.00 234
    2 12345 114,35 2.00 1.00 1.00 201
    3 12345 114,35 14.00 1.00 1.00 600
    4 23412 120.90 110,40 1.00 1.05 700
    5 23412 120.90 12,50 1.00 1.05 900


    In this table, I need to find the setting for each rec_no as
    (r_cost + chg + taxes)-sum (d_cost) for rec_no = 12345
    (114.35 + 1.00 1.00)-(100.30+2.00+14.00) plus 0.05
    This 0.05 must be added to the minimum (code) for the rec_no 12345.
    If the d_cost of the code, with rec_no = 12345 201 should be (2.00 + 0.05 = 2.05).

    and,
    (120.90 + 1.00 1.05)-(110.40+12.50) plus 0.05
    This 0.05 must be added to the minimum (code) for the rec_no 23412.
    If the d_cost of the code, with rec_no = 23412 700 should be (110,40 + 0.05 = 110.45).

    Kindly help me in writing of an oracle application to perform the above operation.
     with tab as (
        select 1 invoice,12345 rec_no,114.35 r_cost,100.30 d_cost,1.00 chg,1.00 tax,234 code from dual union all
        select 2 invoice,12345 rec_no,114.35 r_cost,2.00 d_cost,1.00 chg,1.00 tax,201 code from dual union all
        select 3 invoice,12345 rec_no,114.35 r_cost,14.00 d_cost,1.00 chg,1.00 tax,600 code from dual union all
        select 4 invoice,23412 rec_no,120.90 r_cost,110.40 d_cost,1.00 chg,1.05 tax,700 code from dual union all
        select 5 invoice,23412 rec_no,120.90 r_cost,12.50 d_cost,1.00 chg,1.05 tax,900 code from dual
    )
     SELECT tab.*,
      CASE
        WHEN DENSE_RANK() OVER (partition BY rec_no ORDER BY code)=1
        THEN r_cost + chg +tax-SUM(d_cost) over (partition BY rec_no)+MIN(d_cost) keep (DENSE_RANK FIRST
       ORDER BY code) over (partition BY rec_no)
        ELSE d_cost
      END adj
       FROM tab
    ORDER BY invoice
    

    Find out what...

    Ravi Kumar

  • Query to find the location

    Hello

    How can I find which location has been selected at the time of patching
    is there any query and a front end method to find the same
    It's about the HRMS patches


    Thank you

    .

    Hello

    Please see the following documents (including the reference documents).

    Note: 269705.1 - Question about prerequisite HRGlobal Patch
    How can note: 269286.1 - I find the Version of human resources (HR) on my system?

    Kind regards
    Hussein

Maybe you are looking for