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.

Similar Messages

  • 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

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

  • 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

  • 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

  • Video card to run 2nd and 3rd monitors cs5.5

    Hello all,
    I have a powerful system with multiple hard drives that I use to cut HD DSLR footage on in CS5.5.  My GTX 570 runs my 1st monitor and my 9600GT runs the 2nd and 3rd monitors with great success.
    1st monitor = timeline
    2nd = bins
    3rd = full screen preview (Highest quality preview)
    The reason I use the gtx 570 to run only the first monitor is because it idles much higher and hotter connected to two monitors.  (Disables the down clocking feature on the card when connected to multiple displays).
    Well the 9600gt has died.
    I don't want to spend alot of money here but I am not sure what video card I require for my 2nd and 3rd monitors.  Keep in mind the 3rd monitor is the full screen preview.
    I know premiere pro uses my 570gtx for the MPE but I have no idea how this effects (if it does at all) my 3rd preview monitor since I have had it plugged into my 9600gt.
    There for I am not sure if I can get away with a cheap card like a $50.00 Nvidia GT 610 or do I need something at least equal or greater then my old 9600gt?
    Thanks!,
    Kieran

    Thank you for the reply Harm.  I have an x79 board with a 3930k @ 4.6ghz.
    I will take that advice on board.. I might have to fork out a bit of extra money.  Shame I only purchased the 570 gtx a couple of months ago!
    Found this card....
    Gainward GTX550Ti 1G DDR5 DVI HDMI
    Internet Price:    $111.00
    Barcode  :  
    426018336-2050
    GPU  :  
    GeForce GTX 550 Ti
    GPU Clockspeed  :  
    900 Mhz
    Memory  :  
    1024MB GDDR5 192bits
    Memory Clockspeed  :  
    2050 Mhz
    Pixels per clock (peak)  :  
    N/A
    Bandwidth  :  
    98.4 GB/s
    Ramdac  :  
    400 MHz
    PCI-Express 2.0
    Cooling  :  
    2-Slot Fan
    Video-Features  :  
    HDMI
    Connectivity  :  
    DVI-I + VGA
    Would this be sufficient??
    Thanks!!
    Kieran

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

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

  • 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

  • Dunning 2nd and 3rd letter

    Hi SAP Guru
    How i can get Dunning 2nd and 3rd letters
    I have getten dunning 1st letter but i am not able to get sencond and 3rd
    what is the process , Thank you in advance

    Hi,
    Line items should reach minimum number of days, referring to the due date of net payment, to reach a certain dunning level.
    In FBMP check you dunning interval. If line item has reached interval maintained for 2rd and 3rd level then items will not be dunned.
    Also check dunning levels for your dunning procedure in FBMP.
    If you find no problem with dunning settings then, activate additional log for the dunning run to identify exact reason for the problem.
    Thanks!
    Raju

  • Apple TV (2nd and 3rd Generation) - Home Sharing Not Working but Airplay Does

    Hi everyone,
    This has been a bit of a ongoing problem, so I'll provide a bit of background.
    I am running a Mac Mini (Late 2012) Running Yosemite 10.10.1 as a media server running 10TB of Movies, TV Shows and Music off a NAS Drive. Running iTunes 12.0.1.26. Main hard drive is a 256GB SSD.
    I also have 5 Apple TV's in the house, 2 are 3rd Generation (running iOS 7.0.2), 3 are 2nd Generation (running iOS 6.2.1).
    The goal is to watch the TV Shows and Music through Home Sharing.
    Home sharing works perfectly through the 3rd Generation Apple TV's, as does Airplay. I never have a problem with these. Mac Mini (or any other device for that manner) has no issues accessing the NAS.
    The 2nd Generation Apple TV's are a continual source of frustration. They are connected to the network, can access the iTunes store, can accept Airplay signals from the Mac Mini, iPads, iPhones, etc. Can even detect the other (2nd and 3rd Gen Apple TV's). But frequently, when I try and connect to Home Sharing, I receive the error message "Cannot connect to Home Sharing", even though if I jump on the Mac Mini, I can airplay the content to that Apple TV just fine.
    Is this just a 2nd Generation Apple TV Issue (to make us go an upgrade all our Apple TV's to 3rd Generation)?
    I have completed the following troubleshooting steps, to no success.
    1) Reset the Apple TV's (Multiple Times)
    2) Sign out of Home Sharing\iCloud\iTunes Account, reset, and sign back in.
    3) Restore the Apple TV to the latest firmware and set up again.
    4) Test the device on Wireless and Wired Network.
    5) Reset all network infrastructure.
    6) Power Cycle the Apple TV's for 15 minutes.
    7) Reset iTunes.
    8) Sign out of Home Sharing\iCloud\iTunes Account in iTunes, Reset the Mac Mini, and Sign back in.
    9) Reinstall iTunes.
    10) Reset the Mac Mini and NAS.
    11) Reformat the Mac Mini and update to latest operating system (Mac OS X Yosemite 10.10.1).
    12) Try home sharing from a Windows PC (Windows 8.1, x64).
    Literally out of ideas. Am I chasing a Unicorn on this one? Would love some help.
    Thanks!

    Good to know your Apple IDs are fine.
    So I've been experimenting with de-authorizing and re-authorizing computers and devices and the symptoms you are experiencing are reminiscent of devices and computers unable to authenticate with Apple's servers.
    What I would do is try de-authorizing and re-authorizing computers and devices to see if things return to normal. Start with the computers first. You can quickly de-authorize the computers by logging in to your iTunes Store account and using the 'Deauthorize All' button. After clicking the button wait a few minutes for it to propagate. You will know that the computers have been de-authorize when the Account Information page says "0 computers are authorized to play content purchased with this Apple ID." Other devices like Apple TV and iPads and such do not count on that list.
    Once de-authorized, re-authorize them from the 'Store' menu in the iTunes app. You will know all three computers have been re-authorized when the Account Information page says "3 computers are authorized to play content purchased with this Apple ID." Now try and play your content with Apple TV. If it still doesn't work turn Home Sharing off on the Apple TV and then re-enable it using your Apple ID.
    Hopefully this brings back your Home Sharing functionality.

  • HT1449 I've transferred my itunes library from PC to Macbook pro running latest itunes and now it will not recognize my iPods (nano 4 and 5th Gen an Ipod touch).  All music is there and playable but will not recognize any devices

    I've transferred my itunes library from PC to Macbook pro running latest itunes and now it will not recognize my iPods (nano 4 and 5th Gen an Ipod touch).  All music is there and playable but will not recognize any devices. Help please

    The ipod is not a backup/storage device.
    You need to copy everything from your old computer to your new one.
    The Apple store will do this for you,  you can call apple support and they will help you, you can use google.

  • Ipad 3rd generation does not seem to handle photos very well.  I have a ipad 2nd and iphone 4s, and they both are able to handle photos that my Ipad 3rd can not.

    So here is my experience.  When I first aquired the IPAD 3rd, I did the normal sync with Itunes (windows) and found that very few of my photos came over.  Even though these were the same photos that are on my IPAD 2nd. Same for my phone 4s.  After much consternation, I was able to get photos on the IPAD 3rd, by identifying photos which could not be synced.  These "problem" photos sync fine with my ipad 2nd and iphone 4s.   There is also another issue with IPAD 3rd.  There are photos that take 5-10 seconds or so to "focus".  Even though the focus issue is not apparent on my other devices (ipad 2nd, iphone 4s and windows computer).   The other related issue is with the sync. process.  Syncing photos will quit the entire sync. operation when it encounters a problem photo (with no errors), rather then skipping the bad photo and moving on.  I analyzed some of the problem photos and could not identify any single issue - there are all a little different.  For example one was the result of changing the format options from optimized to standard... but this was not always the case. Others were chaning the format from "Exif JPEG" to "JPEG". while others I could not ever get to sync. In all cases I did a save as using various options.    However, the bottom line was that all of these same photos sync fine and view fine on my ipad 2nd and iphone 4s.   I have not yet figured out what is causing the issues with focus delays.   I use an olympus pen camera (12 meg resolution).

    This works!
    go to settings> general>restrictions>privacy and allow changes.  Make sure that your camera is on under allow!

  • What's the difference between 2nd and 3rd generation

    PLEASE HELP! New Apple user and looking to purchase a used 2nd or 3rd generation ipod touch.
    what's the difference between 2nd and 3rd generation?
    apparently I am told that the 2nd gen does not have the capabilities of listening without headphone? and the 3rd gen does??

    Hi maximumslowness - (great name),
    The 2nd gen Touch has one internal speaker, so you can listen to it, (both left and right channels) without headphones.
    However - it's a small single speaker and it is not very loud. It's not designed for quality listening (neither is the speaker on the 3rd gen as far as I know) and probably will not be adequate if there is a lot of background noise nearby.
    One option is to drop the iPod (not literally!) into an iPod Docking station so that the sound can play out through a self-contained mains-powered amplified speaker, which will also charge your iPod. There are many on the market. On the bottom of the iPod is the 30-pin dock connector which can be used to feed the audio to a docking station. Another option is simply to use a cable to connect from the headphone socket to the audio input on any amplifier.
    Yet another is to plug in a small fold-up style speakers, such as
    http://www.amazon.co.uk/Portable-Folding-Speakers-iPods-Players/dp/B000A1QKNU/re f=sr_1_84?ie=UTF8&qid=1332326622&sr=8-84
    or http://www.amazon.co.uk/Generation-Capsule-iPhone-Laptop-Speaker/dp/B002XNO7FQ/r ef=sr_1_6?ie=UTF8&qid=1332326656&sr=8-6
    I've no idea how good the sound is, you'll have to judge that for yourself. The links are both to a UK store, but most countries will have a similar option.
    I do not recieve any form of payment or compensatiion for the above links.
    Message was edited by: the fiend

Maybe you are looking for

  • Buttons opening a web page within a web page.

    I want to click on a button that will open a web page inside the same web page. How how I do this? Thanks, L

  • Authorization for navigational attribute

    Hi Gurus, I am facing an authorization issue with respect to infoobject hierarchy. I have created authorizations as below. There one infoobject 'A' and a navigational attribute 'B' in infoobject 'A'. This navigational atribure A_B is used in an infoc

  • How to create dblink from oracle to sqlserver

    sqlserver:10.5.0.61 sqlserver 2005 windows server 2003 oracle:10.30.16.19 9.2.0.6 solaris i installed oracle 9i transparent gateway on 10.5.0.61, and the follows are my config files : 10.5.0.61: D:\oracle\ora92\tg4msql\admin\initvatdata.ora # This is

  • Report for Fixed & Variable cost

    Hi Experts, Is there any report where in we can get the value of Fixed costs, varaiable costs & Total costs as in CK13N. We were able to get this for each part in CK13N. Whereas the requirement is we want to see the costs for a list of part nos. Than

  • Cannot type Japanese in a flash field, with latest flash plugin and firefox.

    When I try to input Japanese with my IME(I tried google and Microsoft one)in a flash field, the IME just won't activate. I am stuck with only being able to use western letters and I can't change it in any way. If I load up Opera I am able to input Ja