Oracle 9i SQL: Find nth highest

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

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

Similar Messages

  • Finding nth highest salary

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

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

  • Where to find "oracle.xml.sql.dml.OracleXMLSave"

    I am having trouble finding this file --> import oracle.xml.sql.dml.OracleXMLSave
    Q1: Can someone please tell me where I can find it?
    Q2: Where would I put it (.jar) after I manage to get it? Would I put it in my C:\javasoft\jre\1.2\lib\ext folder?
    Thanks in advance,
    Victoria.
    null

    Thanks, Steven. That is what I thought, and I have included that in my CLASSPATH too. Hmmm ...
    Thanks,
    Victoria.

  • How to debugg and find wrong statements and correct in Oracle PL/SQL

    Hi friends,
    Any1 can guide me, how to debug oracle PL/SQL package, and have to fix bugs or changing the requirement by using toad, sometimes i resolve the problem, and i take backup in notepad, after sometime if i use the same notepad contents, the something changed, i dont no how to keep the "IF loop" correctly, Sometimes while editing, the loop nevigation changes, so that i am getting more confusions. I dont no how to correct the packages already written, and how to solve problems, i use anonymous for seeing output. Dont no how to identify and fix the bug or requirement changes,. I am not sure where the code need changes. How to test properly for fixing the bugs, and how to change the logic, any1 help me out from this,... kindly give me good solution.. thnak you in advance

    Hi,
    you seem a little confused with your previous posts:
    956684 wrote:
    Hi, Friends,
    I have 4+ years experience in oracle plsql developer,
    956684 wrote:I am newly changed the technology into oracle PL/SQL developer,In one (3-Sep-2012) you said you have 4+ years experienced in PL/SQL developer, in the other (17-Nov-2012) you said you are new to PL/SQL developer.
    The kind of question you are posting shows that you have little experience and you have been thrown in a sort of "training on the job" which is a bit scary to my opinion.
    Other users have already posted their opinion. Oracle documentation, available online is a good source of information. I've learnt a lot reading it.
    But what is really important it try to work in a rational way:
    a) understand business , business requirement and data model.
    b) organize your job in a "safe" way. Version control is mandatory!! You cannot avoid it.
    c) Be clean and organized in your coding. Spaghetti coding doesn't lead you anywhere. And never miss documenting your code.
    d) Whenever you have a question constantly check Oracle documentations, Oracle forums, internet. Having web access is a great advantage of our days, you have all you need on your fingertips.
    If I can give you my humble opinion, learning is a slow path. You don't become expert in one day. It's a matter of learning and experiencing day by day.
    One last thing. Writing message like this:
    Any1 can guide me, how to debug oracle PL/SQL package, and have to fix bugs or changing the requirement by using toad, sometimes i resolve the problem, and i take backup in notepad, after sometime if i use the same notepad contents, the something changed, i dont no how to keep the "IF loop" correctly, Sometimes while editing, the loop nevigation changes, so that i am getting more confusions. I dont no how to correct the packages already written, and how to solve problems, i use anonymous for seeing output. Dont no how to identify and fix the bug or requirement changes,. I am not sure where the code need changes. How to test properly for fixing the bugs, and how to change the logic, any1 help me out from this,... kindly give me good solution.. thnak you in advanceis not nice. Your message looks confusing, a single paragraph, approximate English.
    Being clean and organized in your job is shown also in your messages. If you post your message in a clear and well written (and formatted) way you will have more chances to get an answer.
    Regards.
    Al

  • Need To Create a table in Sql Server and do some culculation into the table from Oracle and Sql

    Hello All,
    I'm moving a data from Oracle to Sql Server with ETL (80 tables with data) and i want to track the number of records that i moving on the daily basis , so i need to create a table in SQL Server, wilth 4 columns , Table name, OracleRowsCount, SqlRowCount,
    and Diff(OracleRowsCount - SqlRowCount) that will tell me the each table how many rows i have in Oracle, how many rows i have in SQL after ETL load, and different between them, something like that:
    Table Name  OracleRowsCount   SqlRowCount  Diff
    Customer                150                 150            
    0
    Sales                      2000                1998          
    2
    Devisions                 5                       5             
    0
    (I can add alot of SQL Tasks and variables per each table but it not seems logicly to do that, i tryid to find a way to deal with that in vb but i didn't find)
    What the simplest way to do it ?
    Thank you
    Best Regards
    Daniel

    Hi Daniel,
    According to your description, what you want is an indicator to show whether all the rows are inserted to the destination table. To achieve your goal, you can add a Row Count Transformation following the OLE DB Destination, and redirect bad rows to the Row
    Count Transformation. This way, we can get the count of the bad rows without redirecting these rows. Since the row count value is stored in a variable, we can create another string type variable to retrieve the row count value from the variable used by the
    Row Count Transformation, and then use a Send Mail Task to send the row count value in an email message body. You can also insert the row count value to the SQL Server table through Execute SQL Task. Then, you can check whether bad rows were generated in the
    package by querying this table.  
    Regards,
    Mike Yin
    TechNet Community Support

  • Error: ORA-03113 while executing complex java code from Oracle PL/SQL

    Hi,
    I am trying to execute a complex java code from Oracle PL/SQL. The classes were resolved successfully. But in the middle of execution, I am getting the following error:
    ERROR:
    ORA-03114: not connected to ORACLE
    begin
    ERROR at line 1:
    ORA-03113: end-of-file on communication channel
    Process ID: 13685
    Session ID: 21 Serial number: 20
    Is there a way to debug the Java classes loaded into oracle? Or is there any utility to find out why the connection was lost?
    Regards,
    Saravana

    Hi Saravana:
    You could use simply System.out.println(..) at your Java code and see the output at the .trc files generated for your Oracle session.
    Or better than this adding Java Util Logging messages at your code, to see how to use JUL API at the OJVM please see this blog post [Using JUL API inside the OJVM|http://marceloochoa.blogspot.com/2007/11/getting-logging-entering-exiting-and.html].
    Best regards, Marcelo.
    PD: ora-0600 generally are associated with RDBMS bugs, but these bugs can be bypassed by replacing the code which throws the exception by other with a workaround.

  • How to search this value in oracle database to find out the table

    Hi expert,
    I know there is a value in oracle database, please show me how to search this value in oracle database to find out the table holding this value.
    Many Thanks,

    918440 wrote:
    Hi friends,
    this question is really practical, I already know there is value from application saved in database, I want to search the whole database to figure out which table the value is contained.write SQL that writes SQL to query every table.
    Handle:     918440
    Status Level:     Newbie
    Registered:     Mar 2, 2012
    Total Posts:     20
    Total Questions:     10 (10 unresolved)
    why do you waste time here when you NEVER get any answer to any question you post?

  • Differences between being an Oracle and SQL DBA?

    Sorry to post this here, but I need so insight and don't know any Oracle DBA's. As posted here before, Ive been a MSSQL DBA for about 6 years and want to broaden my skill set. One of my ideas is to learn Oracle. For the record, I do realize that my teaching myself wont open up hundreds or Oracle DBA jobs, but Im hoping to land a "mostly SQL with a little Oracle" gig somewhere. Ive successfully installed Oracle on Linux, and now need to make a decision on if to proceed or not. Any insights from people that have worked with both platforms would be especially helpful, but all thoughts are definately welcomed. So now my questions:
    1. Why are Oracle DBA's paid more than SQL DBA's? It must be something skill specific? What is it?
    2. Will this trend likely continue as newer versions of Oracle are released that are easier to manage?
    3. With that extra cash, is there typically more hours involved? I dont mind working a bit extra if needed, but I do have little ones that I'd like to watch grow up.
    4. Are there typically differences in the job responsibilities between Oracle and SQL DBA's? The changing trend these days is to include DTS/ Analysis Services/ Reporting Services all under the DBA umbrella. Do Oracle DBA's typically have the same type of stuff?
    5. If I do continue down this path, it would probably be far easier for me to learn on Windows than *nix. Is this a valid thing to do, or should I not bother? I've been told that it's still worth while, but have real reservations about needing to tell an interviewer "I've only used it on Windows".
    I realize this is all subject to personal experiences and feelings, but don't know how else to figure this stuff out without asking.
    TIA, ChrisR

    By SQL DBA, I assume you mean SQL Server DBA. SQL is 'Structure Query Language' and is not a specific product, no matter how much the Microsoft community might wish to usurp the term. <g>
    There are mauy reasons for this, including the Oracle vs Microsoft mindset that has been nurtured over the years. For many managers, 'Oracle is solid, reliable and expensive' whereas 'Microsoft SQL Server is cheap, not necessarily as reliable, but good enough'.
    Whether true or not is irrelevant, these are direct quotes from some of my customers.
    My remaining answers are based on my personal experience, and may only be valid in my area. (Yes, things are VERY regional.)
    >
    1. Why are Oracle DBA's paid more than SQL DBA's? It
    must be something skill specific? What is it? Often, an Oracle DBA is a trained DBA. SQL Server DBAs seem to be decent developers who are [or have been stuck] doing DBA work.
    I find that SQL Server DBAs who are truly capable of handling all of {backup, recovery, troubleshooting, security, development, disaster recovery, audit, locking, configuration, RAID and disk, upgrade, migration and porting (to name a few)} competently are generally paid comparable to Oracle DBAs.
    However, I also find there are extremely few true SQL Server DBAs.
    >
    2. Will this trend likely continue as newer versions
    of Oracle are released that are easier to manage?Yes.
    Although Oracle administration is easier, DBAs are now expected to handle more instances, and adminstrator across more servers, and are expected to be competent in more of the feature capabilities.
    There is NO reduction in the amount of work. The 'easier to manage' stuff simply means being more efficient.
    >
    3. With that extra cash, is there typically more
    hours involved? I dont mind working a bit extra if
    needed, but I do have little ones that I'd like to
    watch grow up.
    A blatant observation: Oracle tends to be used in more mission critical environments, and SQL Server in department support environments. Mission critical tends to be watched closer, and require 'fast aqnd competent' response. Pager duty seems to be more prevelant in Oracle positions - department server crashes can be handled by a reboot in the morning.
    4. Are there typically differences in the job
    responsibilities between Oracle and SQL DBA's? The
    changing trend these days is to include DTS/ Analysis
    Services/ Reporting Services all under the DBA
    umbrella. Do Oracle DBA's typically have the same
    type of stuff?Many SQL Server 'DBAs' I know personally are 'super developers' but not really DBAs.
    Observation: DBAs are often very 'repeatable' detail oriented, whereas developers are 'get it done' oriented. Very different mentailties, both very important.
    >
    5. If I do continue down this path, it would probably
    be far easier for me to learn on Windows than *nix.
    Is this a valid thing to do, or should I not bother?
    I've been told that it's still worth while, but have
    real reservations about needing to tell an
    interviewer "I've only used it on Windows". Oracle is quite operating system independant. Pick your poison.
    SQL Server is quite operating system dependant. Some else picks your poison.
    There is no shame in saying to an interviewer that you have a strong competence in Oracle but only experience in Windows. A good potential employer will often provide appropriate additional training to a suitable, honest, candidate.
    >
    I realize this is all subject to personal experiences
    and feelings, but don't know how else to figure this
    stuff out without asking.
    I strongly encourage you to contact locals rather than ask in these forums. Check whether there is an Oracle User Group close to you, and meet and chat with the members. Ditto for SQL Server. (You can find out by contacting respective sales reps.)
    The above is NOT a dismissal of SQL Server capabiltites or SQL Server DBAs. (Some close friends are extremely competent with SQL Server.) It is just a set of personal observations.

  • Oracle.xml.sql.query.OracleXMLQuery   GetXML -withDTD

    I'm trying to create a dtd for each table in my schema. I've created a java program that loops through the user_tables and I am then trying to create the DTD with oracle.xml.sql.query.OracleXMLQuery -withDTD. I am able to make it work using the XSU Command Line Utility, but I'm having a hard time trying to find out where the -withDTD flag goes inside my java program. Anyone know how to use the -withDTD option inside of a java program?
    Thanks

    [from XmlRpc-Java]
    As a result, my XML
    is contained in a String instead of an InputStream.
    It is possible to convert a String to an InputStream using
    StringBufferInputStream, but this class is deprecated in favor of
    StringReader (as StringBufferInputStream does not properly convert
    characters into bytes).
    For the default HTTP transport, an InputStreamReader could be used to
    wrap the InputStream before calling parse().
    Ultimately, the InputStream is converted to an InputSource for use by
    the SAX parser. InputSource will accept a Reader as well.
    [from XmlRpc-Java]
    There should be a way to go from "getXMLString()" to "parse(Reader)" also. In the meantime I'm stuck.
    null

  • Oracle.xml.sql.query.OracleXMLQuery not found

    Hi!
    Sorry, but I got a project to continue and there is an import statement "import oracle.xml.sql.query.OracleXMLQuery;" and this gives me a compilation error...
    What do I need to do, where do I find this class??? Any help?
    //Patricia

    Patricia,
    Did you go through the link
    Re: XML SQL Utility
    You have to put xsu12.jar in the lib directory of the jdev.
    xsu12.jar is in the lib directory of the XDK installation.
    You can download XDK from
    http://www.oracle.com/technology/tech/xml/xdk/software/prod/xdk_java.html
    Just download the XDK kit, get the xsu12.jar from the lib directory and put in the lib directory of the jdev.
    -- Arvind

  • Oracle.xml.sql.query.OracleXMLQuery?

    Anyone know where I can find a java doc or any method infomation on:
    oracle.xml.sql.query.OracleXMLQuery
    thanks,
    chad.

    [from XmlRpc-Java]
    As a result, my XML
    is contained in a String instead of an InputStream.
    It is possible to convert a String to an InputStream using
    StringBufferInputStream, but this class is deprecated in favor of
    StringReader (as StringBufferInputStream does not properly convert
    characters into bytes).
    For the default HTTP transport, an InputStreamReader could be used to
    wrap the InputStream before calling parse().
    Ultimately, the InputStream is converted to an InputSource for use by
    the SAX parser. InputSource will accept a Reader as well.
    [from XmlRpc-Java]
    There should be a way to go from "getXMLString()" to "parse(Reader)" also. In the meantime I'm stuck.
    null

  • Oracle 8i SQL and PL/SQL plus

    hi everybody, i am trying from two weeks to install oracle 8i SQL and PL/SQL plus on my PC on both Windows 2000 & XP and I installed every java runtime environment I found "the old and the new" but the SETUP insists on not working though it worked on the instructor's PC smoothly without problems.

    If you are getting any errors, you should tell us. Also, you should let us know what version you are trying to install.
    A very common problem at the moment is with Pentium 4 processors. If you have a Pentium 4, search the forums to find the answer.
    Alison

  • Java.lang.NoClassDefFoundError: oracle/xml/sql/query/OracleXMLQuery

    Hello, all.
    I get this error message:
    java.lang.NoClassDefFoundError: oracle/xml/sql/query/OracleXMLQuery
    at oracle.xml.xsql.actions.XSQLQueryHandler.handleAction(Compiled Code) ...
    when trying to view an xsql page with the jswdk 1.0.1 web server. (I have no problems when using Web-to-go)
    Classpath includes:
    C:\jdk1.1.8\lib\classes.zip;
    C:\xsql\lib\oraclexsql.jar;
    C:\xsql\lib\xmlparserv2.jar;
    C:\xsql\lib\xsu111.jar;
    C:\xsql\lib\classes111.zip;
    C:\xsql\lib;
    What could be the problem?
    Mateja
    null

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by Steven Muench ([email protected]):
    Only thing I can think of is that maybe your server classpath is getting too long. I recall one of the Java Web Server releases having a classpath length limit that caused strange errors like this because that .jar files you thought were on your classpath were getting their path names truncated so the Java VM cannot find the JAR's.
    Try putting xsu111.jar earlier in the list of JAR's and/or try shortening the classpath (perhaps by using SUBST'd drive letters or softlinks on Unix to shorten the path names).<HR></BLOCKQUOTE>
    Putting the xsu11.jar file towards the start of the path did not help.
    I have rewriten the entire bat file that creates the CLASSPATH and starts the server and things now seem to work. There must have been some error in the original bat file, that I just couldn't see.
    Anyway - I thank you for your help.
    null

  • How to use Oracle 10G SQL features in OWB?

    We are using OWB repository 10.2.0.1.0 and OWB client 10.2.0.1.31. The Oracle version is 10 G (10.2.0.3.0). OWB is installed on Linux.
    Oracle 10G SQL offers
    1) Inserting Into a Table with Error Logging
    2) conditional_insert_clause, Specify the conditional_insert_clause to perform a conditional multitable insert.
    Did anyone use above features in OWB10G mapping?
    How do we use them?
    Thanks in advance.
    RI

    Hi,
    Just a thought on the multi-table insert. OWB generates multitable insert when one source is mapped to multiple targets only on the condition that after the splitter there should not be any OWB operator that can change the cardinality of the row-set coming out of the splitter (like joiners, filters, aggregators etc.)..
    Regarding the link, I was wondering where I could find a decent documentation of the shadow table and the DML Error Logging through OWB .. Thanks for the link.
    OWB error messages are basically Oracle Error Messages when you consider the code that gets deployed. Other than that VLD errors are sometimes mysterious and hard to debug.. Hope someone can give us more details on how to handle such VLD errors :)
    HTH
    -AP

  • Oracle and SQL Server connectivity

    Hello,
    Currently, we use Oracle as our enterprise database. I have a new requirement to develop a new application that must use MS SQL Server which must push and pull data from the Oracle database.
    What is the best technical solution to have this communication between Oracle and SQL Server.
    Thank you.

    if you are connecting to SQL Server from Oracle then yes HS through ODBC (check HS forum), but if you are pulling data from SQL server you have an option of DTS or "shared" server through ODBC, see MS documentation for that......you won't find it on oracle forum......
    Eugene Roytman

Maybe you are looking for

  • Is it possible to point my itunes library to external HD

    I have limited space on my laptop. I have a large HD connected on my network that I have a lot of media on. Can I tell iTunes to point to that folder for movies or TV shows instead of having the media on my local drive. The main reason is so I can st

  • Copying Image to clipboard

    I would like to copy graphics (e.g. plot or table) in the application to the SystemClipboard. The goal is to be able to transfer this image to the other application. It is rathewr simple to copy text, however it is not clear how to copy graphics. Any

  • Object reading and writing

    i have to read a very large files that consist of header(5000 bytes)main file(10,000 bytes), footer(4000 bytes) this is a binary file.The current code is in c++, the logic implemented is like as:- there are three classes and the data structure define

  • 100% to 65% Battery Level Decrease in Two Hours

    My question is: "Is anyone else seeing rapid battery level drop with iPhone since upgrading to IOS 8?" Since upgrading to IOS 8 on Wednesday the 17th, I have experienced RAPID battery level drop between charges.  I am now having to charge my iPhone 5

  • Will 4 yr old macbook accept snow leopard

    I work as a musician on a cruise and have a friend who's computer, a 4 yr. old macbook 10.4 needs re-installing of the system. His discs are at home. On the ship I have snow leopard and all of my install disks. 10.6. Will these work on his older comp