2nd and 5th highest salary

Hi, I'm kind of new to sql. I have a table with the name of the employee and salary.
I would like to find the name of the 2nd higher salary? also I want to get the name of the highest salary 5th
Here's my data

INSERT INTO er4 VALUES ('John', 1000, 123);
INSERT INTO er4 VALUES ('john2', 3000, 234);
INSERT INTO er4 VALUES ('Jean3', 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, there are two employees with the second highest salary. I would like to get one or the other, but I want my query to return a single line

can someone help me write a query to get the name and salary of the 2nd higher and the name and salary of 5th higher

Hello

Part of learning about the functions is to learn 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 may be able to 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, as Justin did.
Why? The following scenario happens all the time in real life. The client for which you wrote this comes back in 3 months and said: 'this query you wrote for the highest salary works very well! We found it so useful, we would like to do something similar to find the 2nd highest salary, either 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 a problem, you'll save time (long-term) If you can predict, or just guessing, what you need to solve later and write something for the problem today that can easily be adapted for potential future similar problems problems.

Tags: Database

Similar Questions

  • How can I get the second and third highest salary from the emp table

    How can I get the second and third highest salary from the emp table in the ecah Department
    SQL> ed
    Wrote file afiedt.buf
    
      1  select empno, ename, sal, deptno
      2  from (
      3    select empno, ename, sal, deptno, dense_rank() over (partition by deptno order by sal desc) as rn
      4    from emp
      5    )
      6* where rn in (2,3)
    SQL> /
    
         EMPNO ENAME             SAL     DEPTNO
    ---------- ---------- ---------- ----------
          7782 CLARK            2450         10
          7934 MILLER           1300         10
          7566 JONES            2975         20
          7876 ADAMS            1100         20
          7499 ALLEN            1600         30
          7844 TURNER           1500         30
    
    6 rows selected.
    
    SQL>
    
  • 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 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
    
  • Config of basis for the 2nd and 3rd of the WLC?

    I saw the discussion about the configuration of the failover on of the WLC. I think I have a pretty good understanding of what is supposed to happen here. But what is really clear is the config of base on the 2nd and 3rd in WLC. They need to be configured exactly like the first, with the exception of the unique fields such as host name and ip addresses, interface and such? Usually people take the config of the first and do a "Find and replace" to fix the config for subsequent controllers? I will add 2 more to my controller in the near future and try to have a better understanding of the process until I have to implement. Thank you!

    You are right in the config WLC - unique IP/hostname info and everything else the same. There is usually not a lot of changes of configuration to do on the additional WLC, the few times that I did I have manually configured things or used WCS. Configure additional WLC being part of the same group of mobility and/or hardcode primary, secondary & tertiary controllers AP for failover.

    HTH

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

  • 2nd highest salary

    How to get the second salary scott.emp hieghest?

    It would be helpful

    Select ename from scott.emp one where 1 = (select count (1) Scott.) B WHERE THE A.SAL EMP<>

  • Question of Layout VBOX with a few pixels for components of 2nd and 3rd shift.

    The VBox has 2 components, by clicking on the label of the rocker to the left.

    a component is hidden, and the other is made visible.

    How does one get rid of the padding at the top when the

    2nd component is displayed.

    At the end of the script are two images with one showing the small padding.

    ------------------------------------------------------------------------------------------ -------

    <? XML version = "1.0" encoding = "utf-8"? >

    " < = xmlns:fx s:Application ' http://ns.Adobe.com/MXML/2009 "

    xmlns:s = "library://ns.adobe.com/flex/spark".

    xmlns:MX = "library://ns.adobe.com/flex/mx" minWidth = "955" = "600" minHeight >

    < fx:Script >

    <! [CDATA]

    protected function lblSearch_clickHandler(event:MouseEvent):void

    {

    TODO self-generating method stub

    _Debug ("inside" + functionName());

    if(tabnavDetails.Visible == true) {}

    tabnavDetails.visible = false;

    tabnavSearchDetails.height = tabnavDetails.height;

    tabnavSearchDetails.width = tabnavDetails.width;

    tabnavDetails.height = 0;

    tabnavDetails.width = 0;

    tabnavSearchDetails.visible = true;

    } ElseIf (tabnavSearchDetails. visible = true) {}

    tabnavSearchDetails.visible = false;

    tabnavDetails.height = tabnavSearchDetails.height;

    tabnavDetails.width = tabnavSearchDetails.width;

    tabnavSearchDetails.height = 0;

    tabnavSearchDetails.width = 0;

    tabnavDetails.visible = true;

    }

    }

    []] >

    < / fx:Script >

    < fx:Declarations >

    <! - Place non-visual elements (e.g., services, items of value) here - >

    < / fx:Declarations >

    < mx:HBox height = "100 percent" width = "100%" >

    < s:Label id = "lblSearch" name = "lblSearch" buttonMode = "true" text = "Toggle" color = "#2133E4" height = "5%" paddingTop = "8" click = "lblSearch_clickHandler (event)" / >

    < mx:VBox id = "vboxMainBody" name = "vboxMainBody" width = "80%" height = "95%" borderColor = "Black".

    borderStyle = "solid" borderVisible = "true" focusColor = "#FAFAFA" >

    < mx:TabNavigator id = "tabnavDetails" name = "tabnavDetails" visible = "true" borderVisible = "true" width = "100%" height = "100%" >

    < s:NavigatorContent width = "100%" height = "100%" label = "Media details" >

    < / s:NavigatorContent >

    < s:NavigatorContent width = "100%" height = "100%" label = "Location and eyeballs" >

    < / s:NavigatorContent >

    < / mx:TabNavigator >

    < mx:TabNavigator id = "tabnavSearchDetails" name = "tabnavSearchDetails" visible = "false" borderVisible = "true" width = "0%" height = "0%" paddingTop = "0" >

    < s:NavigatorContent width = "100%" height = "100%" label = "Search Details" >

    < / s:NavigatorContent >

    < s:NavigatorContent width = "100%" height = "100%" label = "Location and eyeballs" >

    < / s:NavigatorContent >

    < / mx:TabNavigator >

    < / mx:VBox >

    < / mx:HBox >

    < / s:Application >

    ------------------------------------------------------------------------------------------ -------

    img1.jpgimg2.jpg

    MX:VBox verticalGap = '0 '.

  • Firefox very slow to open windows of 2nd and 3rd

    When I click on the icon of Firefox Firefox / Google homepage opens in 2 seconds and I can select an item in the bookmarks.
    If I try to open a second, it takes about 20 seconds, and the following applications take progressively longer.
    It is a nuisance that I regularly need to have three or four points at the same time.

    Try to clear your recent history and see if it works

  • Acrobat 9 Pro - layout - 1st page to display unique, 2nd and 3rd double

    An 8 page newsletter has been created with Microsoft Publisher and exported in PDF format. I have Acrobat 9 Pro and to view the document in a manner similar to how it appears in the editor.

    Editor has an option of two pages that allows the first page of the newsletter to be considered to be a single page and pages 2 & 3 to display side by side as if the newsletter was opened. I would like to have this same point of view in Acrobat.

    In Acrobat, I can go to view > Page display and select two until you see two pages side by side, but so far I have done all the pages seen side by side... I need the first and last page are considered to be a single page, so that all the intermediate pages are considered to be two upward.

    Where in Acrobat adjust the setting to allow the first and last page to be considered unique pages, while all the intermediate pages are considered to be two Up?

    Thank you

    View - Page Display - tick "Appear on the cover page during two"

  • HP Pavilion a6077c desktop PC

    I own a HP Pavilion a6077c desktop PC and just installed Windows 7 x 64, when I went on HP website to find drivers audio and clicked on my operating system like Windows 7 64 x happens only with a Lightscribe driver, so I clicked on the Windows Vista 64 x audio driver and it does not work I have some more thoughts on what it takes to please respond.

    Based on the specifications of your computer, you need the following drivers;

    Realtek HD Audio Codec - click 'I agree', click 'next' and download the 2nd and 5th point to the bottom of the list

    Intel Chipset Device Software

    Driver Intel GMA (graphics)

    Intel PROSet network driver

    Frank

  • Strange behavior of the cursor to the row in the table

    Hello.

    I use JD Studio Edition Version 11.1.1.5.0 on Weblogic 11.1.1.5

    I have page JSPX in taskflow bounded to view history records in the table.

    DB table is simply with a single primary key and number of columns.

    On the page, I show only little of them (Date, user, Rec.Number and State).

    When I open this dialog box with records that hold these documents

    ID Date user RecNumber State

    67

    02.01.07User111

    68

    02.01.07User122

    69

    02.01.07User131

    70

    02.01.07User142

    71

    02.01.07User151
    7202.01.07User162

    I see highlighted records with status = 1. Click on any record highlighting disappears and when I try to move the cursor down through the cursor of the keyboard (arrow keys) Stoops (pressing "down") until that record 3rd then skip (press 'down' key) 2nd record and repeat this. When I try to move the top slider to 5th/4th then folder and back loop.

    When I leave the body of the table and click outside the table then highlighting appears again.

    If I show only records State = 1 then all records are highlighted and the cursor jumps between 2nd and 5th record.

    ADF table is created simply by dragging and dropping of DataControl with RowSelection simple =, rowBandingInterval = 0, filterVisible = "true".

    I can find no reason for this kind of behavior.

    Please help me understand what can be the reason of this madness.

    Thanks in advance

    I check the original Version and compare it with the last good version.

    I found at the end

    This section is missing from the right version

    I add

    and everything is fine now.

    The same problem was in an another PK VO. missing base table.

  • How to extract folder 5 Albums

    Hello

    I have a requirement to get the employee who gets the 5th highest salary.

    I don't want to use query analysis here.

    I remember writing a query using ' select * from emp where 5 > (select...) »
    Now I forgot logic, please help, thank you.

    Well, it depends on how your data are defined.

    For example if your data are as mentioned below.

    emp table -
    
           ENO     SALARY
    ---------- ----------
            10       1000
            20       2000
            30       3000
            40       4000
            50       5000
            60       6000
            70       7000
            80       8000
            90       9000
    

    In this case, the following query will work-

    Connected to Oracle Database 11g Release 11.2.0.2.0 
    
    SQL>
    SQL> SELECT eno
      1        ,salary
      2    FROM emp a
      3   WHERE 5 = (SELECT COUNT(*)
      4                FROM emp b
      5               WHERE b.salary >= a.salary) -- Please note the sign ">="
      6  /
    
           ENO     SALARY
    ---------- ----------
            50       5000
    
    SQL> 
    

    So if you are sure that column salary will have unique values, then you can use above query probably. However, this query fails for a few scenarios what column salary contains extensible data (which are possible for most of the implementation for the salary column). Two of these scenarios are
    s ' is used mutiple with 5th highest salary i.e. 5000 in the example above
    If ' there are several employees with the 3rd highest salary i.e. 3000 in the example above.

    examine the data mentioned below. There are several employees with 5th highest salary that is 5000. So expect multiple records output. Above mentioned query will fail for such data. So is the modified query in order to manage these data-

    emp table -
    
           ENO     SALARY
    ---------- ----------
            10       1000
            20       2000
            30       3000
            40       5000        -- salary changed to 5000
            50       5000
            60       6000
            70       7000
            80       8000
            90       9000
    

    In this case, the following query will work-

    Connected to Oracle Database 11g Release 11.2.0.2.0 
    
    SQL>
    SQL> SELECT eno
      1        ,salary
      2    FROM emp a
      3   WHERE 5 = (SELECT COUNT(DISTINCT salary )
      4                FROM emp b
      5               WHERE b.salary >= a.salary)
      6  /
    
           ENO     SALARY
    ---------- ----------
            40       5000
            50       5000
    
    SQL> 
    

    Top query also works for other combinations of data. For example if there are several employees with salary 7000 which is greater than 5 the higher salary. In other words, if there are several employees with the 3rd highest salary, then also there is not any change in the production of the above query. If you want the PERFECT query (without using the analytical functions) then second above application so should be the choice.

    I hope this helps.

    See you soon,.
    Gregory

  • Equium P300-190 and P300-16 t - where to find cases of HD 2nd slot

    Does anyone know where to find the case of hard drive to the right side, and eager to put another hard drive in the second slot, but the case has 5 plastic post and don't want to break them.

    Anyone use the second location for an additional hard drive?

    You should check if the laptop supports a 2nd HARD drive.
    The availability of 2nd HARD disk location does not mean that the laptop would support a 2nd HARD drive.
    You should remove the HARD drive cover in the 2nd and need to check if the location contains the connector to HARD drive.
    If this isn t available connector, you cannot use 2nd HARD drive

  • I have ipod touch? 3rd and 2nd generation? The serial number is 1 has 932283201

    I looked through all the descriptions of the 2nd and 3rd generations, but I don't know yet. The serial number is 1 has 932283201

    A second.

    (137235)

Maybe you are looking for