selecting max 5 salaries in the emp table

Hi I tried the following query to obtain the emp table max 5 salaries...
SQL> select sal
  2  from (select sal,empno from emp order by sal desc
  3  where rownum<6;

       SAL
----------
      5000
      3000
      3000
      2975
      2850
But here in the output of the duplicate rows are there... I need to remove this and number total should be equal to only 5...
If I put the distinct clause, then I'll take 4 rows only... but I need the salaries of the top 5...

Thank you
select *
  from (
select distinct sal
  from emp
 order by sal desc
 ) where rownum <= 5
;

Tags: Database

Similar Questions

  • How to insert 10,000 records test data into the emp table

    Hi I am new to oracle can someone please help me write a program so that I can insert the test data into the emp table
    SELECT       LEVEL empno,
                 DBMS_RANDOM.string ('U', 20) emp_name,
                 TRUNC (DBMS_RANDOM.VALUE (10000, 100000), 2) sal,
                 DBMS_RANDOM.string ('U', 10) job,
                 TO_DATE ('1990-01-01', 'yyyy-mm-dd')
                 + TRUNC (DBMS_RANDOM.VALUE (1, 6000), 0)
                   hiredate,
                 CASE
                   WHEN LEVEL > 10 THEN TRUNC (DBMS_RANDOM.VALUE (1, 11), 0)
                   ELSE NULL
                 END
                   mgr,
                 TRUNC (DBMS_RANDOM.VALUE (1, 5), 0) deptno
    FROM         DUAL
    CONNECT BY   LEVEL <= 10000
    
  • 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>
    
  • SELECT on the emp table query

    Hello


    How to find the display the o/p as a manager name under employees dependent child parent like relation ship on the noraml emp table:

    sample o/p:

    use of name
    Nativity scene XX
    sales of yy
    YY1 sales
    Manager of the AA
    marketing of RR
    RR1 marketing


    Thank you

    921306 wrote:

    So I need, like that, child-parent relationship between the Manager under employees...

    Then join the parent with the child entity entity to create the parent-child relationship.

    The two entities are in the same table, it means to join the table to itself. For example

    SQL> select
      2          child.ename                     as CHILD,
      3          child.job                       as CHILD_JOB,
      4          nvl(parent.ename,'')      as PARENT,
      5          parent.job                      as PARENT_JOB
      6  from emp parent,
      7       emp child
      8  where child.mgr = parent.empno (+)
      9  /
    
    CHILD      CHILD_JOB PARENT     PARENT_JO
    ---------- --------- ---------- ---------
    FORD       ANALYST   JONES      MANAGER
    SCOTT      ANALYST   JONES      MANAGER
    JAMES      CLERK     BLAKE      MANAGER
    TURNER     SALESMAN  BLAKE      MANAGER
    MARTIN     SALESMAN  BLAKE      MANAGER
    WARD       SALESMAN  BLAKE      MANAGER
    ALLEN      SALESMAN  BLAKE      MANAGER
    MILLER     CLERK     CLARK      MANAGER
    ADAMS      CLERK     SCOTT      ANALYST
    CLARK      MANAGER   KING       PRESIDENT
    BLAKE      MANAGER   KING       PRESIDENT
    JONES      MANAGER   KING       PRESIDENT
    SMITH      CLERK     FORD       ANALYST
    KING       PRESIDENT 
    
    14 rows selected.
    
    SQL> 
    

    The basic concept is easy - set the parent entity, set the child entity, join the parent to the child.

    If some entities will not have parents (root entities), then an outer join is required of the child entity to the parent entity.

    Everything simply because the two entities are in the same table, does not change the logic or the approach. It's perfectly valid for a join table.

  • cloumn on the emp table online

    Hello world

    I want to do EMP table in columns by departments
    under the name of employees of each Department

    for example,.

    10. 20
    CLARK | SMITH
    KING | FORD
    MILLER | ADAMS

    Thank you

    You mean like this?

    SQL> select * from emp;
    
         EMPNO ENAME      JOB              MGR HIREDATE                    SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- -------------------- ---------- ---------- ----------
          7369 SMITH      CLERK           7902 17-DEC-1980 00:00:00        800                    20
          7499 ALLEN      SALESMAN        7698 20-FEB-1981 00:00:00       1600        300         30
          7521 WARD       SALESMAN        7698 22-FEB-1981 00:00:00       1250        500         30
          7566 JONES      MANAGER         7839 02-APR-1981 00:00:00       2975                    20
          7654 MARTIN     SALESMAN        7698 28-SEP-1981 00:00:00       1250       1400         30
          7698 BLAKE      MANAGER         7839 01-MAY-1981 00:00:00       2850                    30
          7782 CLARK      MANAGER         7839 09-JUN-1981 00:00:00       2450                    10
          7788 SCOTT      ANALYST         7566 19-APR-1987 00:00:00       3000                    20
          7839 KING       PRESIDENT            17-NOV-1981 00:00:00       5000                    10
          7844 TURNER     SALESMAN        7698 08-SEP-1981 00:00:00       1500          0         30
          7876 ADAMS      CLERK           7788 23-MAY-1987 00:00:00       1100                    20
          7900 JAMES      CLERK           7698 03-DEC-1981 00:00:00        950                    30
          7902 FORD       ANALYST         7566 03-DEC-1981 00:00:00       3000                    20
          7934 MILLER     CLERK           7782 23-JAN-1982 00:00:00       1300                    10
    
    14 rows selected.
    
    SQL> ed
    Wrote file afiedt.buf
    
      1  with d1 as (select ename, row_number() over (order by empno) as rn from emp where deptno = 10)
      2      ,d2 as (select ename, row_number() over (order by empno) as rn from emp where deptno = 20)
      3      ,d3 as (select ename, row_number() over (order by empno) as rn from emp where deptno = 30)
      4  --
      5  select d1.ename as dept10, d2.ename as dept20, d3.ename as dept30
      6  from   d1 full outer join d2 on (d2.rn = d1.rn)
      7*           full outer join d3 on (d3.rn = nvl(d2.rn,d1.rn))
    SQL> /
    
    DEPT10     DEPT20     DEPT30
    ---------- ---------- ----------
    CLARK      SMITH      ALLEN
    KING       JONES      WARD
    MILLER     SCOTT      MARTIN
               ADAMS      BLAKE
               FORD       TURNER
                          JAMES
    
    6 rows selected.
    

    As already mentioned, in a reporting tool is much better.

  • How to display data from the emp table?

    Hi everyone, this is my first post in this portal. I want to display the details of the table emp... for this I use this SQL statement.

    Select * from emp where mgr = nvl(:mgr,mgr);

    When I give the entry as 7698 it shows the matching records... and also when I don't give any input then it shows all the records except the Archbishop with null values.

    (1) I want to display all the records when I do not give any input, including null values
    (2) I want to display all the records that a Bishop is null

    Is it possible to integrate in order to include all these in a single query.

    Hello

    937440 wrote:
    Hi everyone, this is my first post in this portal.

    Welcome to the forum!
    Don't forget to read the forum FAQ {message identifier: = 9360002}

    I want to display the details of the table emp... for this I use this SQL statement.

    Select * from emp where mgr = nvl(:mgr,mgr);

    When I give the entry as 7698 it shows the matching records... and also when I don't give any input then it shows all the records except the Archbishop with null values.

    (1) I want to display all the records when I do not give any input, including null values
    (2) I want to display all the records that a Bishop is null

    Is it possible to integrate in order to include all these in a single query.

    It's a little unsure what you're asking.
    The following query always includes the lines where mgr is NULL and when the binding variable: mgr is NULL, it displays all the lines:

    SELECT  *
    FROM     emp
    WHERE     LNNVL (mgr != :mgr)
    ;
    

    In other words, when: 7698 = mgr, it displays 6 rows and when: mgr is NULL, it displays 14 rows (assuming you are using the table provided by Oracle scott.emp).

    The following query includes lines where Bishop is ZERO only when the binding variable: mgr is NULL, in which case it shows all lines:

    SELECT     *
    FROM     emp
    WHERE     :mgr     = mgr
    OR       :mgr       IS NULL
    ;
    

    When: mgr = 7698, this will display 5 rows and when: mgr is NULL, it displays 14 rows.

    The following query includes lines where Bishop is ZERO only when the link variab; e: mgr is NULL, in which case it shows only those rows where mgr is NULL. In other words, it treats NULL as a value:

    SELECT     *
    FROM     emp
    WHERE     DECODE ( mgr
                , :mgr, 'OK'
                )     = 'OK'
    ;
    

    When: mgr = 7698, this will display 5 rows and when: mgr is NULL, it displays 1 row.

  • minimum hiredate from the emp table

    Hello

    There is a table EMP, hiredate column (date). I want to get the record with minimum hiredate and using this query.
    SQL> select * from v$version;
    
    BANNER
    ----------------------------------------------------------------
    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
    PL/SQL Release 10.2.0.4.0 - Production
    CORE    10.2.0.4.0      Production
    TNS for Solaris: Version 10.2.0.4.0 - Production
    NLSRTL Version 10.2.0.4.0 - Production
    
    SQL> with t as
      2  (select 'A' ename, 1 enum, '1-Oct-2008' hiredate from dual union all
      3  select 'B', 2, '1-Jan-2002' from dual union all
      4  select 'C', 3, '1-Mar-2008' from dual)
      5  select * from t
      6  where hiredate = (select min(hiredate) from t);
    
    E       ENUM HIREDATE
    - ---------- ----------
    B          2 1-Jan-2002
    
    SQL> 
    Is there another way to do it, without using subquery?

    Kind regards
    Ritesh

    Another option:

    SELECT  MIN(ENUM)       KEEP (DENSE_RANK FIRST ORDER BY HIREDATE)       AS ENUM
    ,       MIN(HIREDATE)   KEEP (DENSE_RANK FIRST ORDER BY HIREDATE)       AS HIREDATE
    FROM    t
    

    EDIT: See the post of John Spencer as it has a more in-depth explanation. This will only return the FIRST record oracle meets with minimal hiredate. It will not return more records if more than one employee has the same minimum hiredate.

    Published by: Centinul on October 23, 2009 09:38

  • sum of sal and comm in the emp table

    Hi all

    for the table emp with enam, eno, dept, sal and comm fields

    I want to display a field in oracle forms 6i, who will show me the sum of sal and comm

    I've created a field of no database
    and wrote this code in the formula
    declare
    summ number(10);
    begin
    
    summ=:sal + :comm
    end;
    but I get the compilation failed

    kindly Guide

    thanking in advance

    Hello

    your code

    declare
    summ number(10);
    begin
    
    summ =:sal + :comm  --this line must be  summ :=  nvl(:sal,0) + nvl(:comm ,0)
    end;
    

    Try this code to a trigger
    change after exp.

    declare
    summ number(10);
    begin
    
    summ := nvl(:sal,0) + nvl(:comm,0)
    end;
    

    or send me your complete code.
    Thank you

  • get the cumulative sum of the salaries of the employees of emp table

    Hi gurus,

    Can any body tell me how to get the cumulative sum of the salaries of the employees in the emp table using analytical functions.

    Thanks in advance...

    Please read the document

    http://download.Oracle.com/docs/CD/B19306_01/server.102/b14200/functions163.htm#sthref2186

    Look for example at the bottom of the document.

  • Returns the Nested table Max length

    Hello

    I want to create a function that accepts a nested table / array as I / p and return the maximum length of the elements in the array.

    As if I was passing the table as arr('India','Mumbai','Kolkata','Pune'), this function should return the length max 7 (Kolkata).

    Can apply us the aggregation on the nested table function or is there an alternative?

    Kind regards
    Rakesh

    Hello

    Here's a possible solution:

    
    create or replace type tCharArray is table of varchar2(1000);
    
    create or replace function get_max_size(p_array tCharArray) return number
    is
      l_result number;
    begin
      select max(length(column_value))
      into l_result
      from table(p_array);
      return l_result;
    end;;  
    
    select get_max_size(tCharArray('abc', 'aaaaa')) from dual;
    

    Best regards
    Nikolai

  • Select max (length) - how to display the length of a group MAX

    Hello

    I have a table with the name of DVD and names of files on the DVD.
    I would like to know how to change this query to get another column, MAX_LENGHT, the MAXIMUM length of the name of FILE to a DVD of return
    This doesn't work request
    select DVD, FILENAME, (SELECT max(length(FILENAME)) / 2 + 4 FROM TABLE) AS MAX_LENGHT from TABLE
    DESIRED RESULT
    DVD    FILENAME     MAX LENGHT
    DVD1     Alina     8
    DVD1     Aidan     8
    DVD1     Aiden     8
    DVD1     Akira     8
    DVD1     Alex     8
    DVD1     Alyssa     8
    DVD1     Arianna     8
    DVD1     Ashley     8
    DVD1     Ava     8
    DVD1     Benjamin8
    DVD1     Bianca     8
    DVD1     Blake     8
    DVD1     Brandon     8
    DVD1     Brayden     8
    DVD1     Brayden     8
    DVD1     Brianna     8
    DVD1     Brielle     8
    DVD1     Brooklyn8
    DVD2     Dakota     11
    DVD2     Dalia     11
    DVD2     Daniel     11
    DVD2     Dante     11
    DVD2     David     11
    DVD2     Diego     11
    DVD2     Dingbang11
    DVD2     Dominic     11
    DVD2     Dylan     11
    DVD2     Chase     11
    DVD2     Chloe     11
    DVD2     Christopher     11
    DVD2     Claire     11
    DVD2     Cole     11
    DVD2     Connor     11
    Thank you

    Roseline

    You can use the analytic version of the function max:

    SELECT  DVD
    ,       FILENAME
    ,       MAX(LENGTH(FILENAME)) OVER (PARTITION BY DVD) AS MAX_LENGTH
    FROM    TABLE
    
  • EMP table query

    Hi friends,

    Please help me get the following output from the EMP table:
    DEPTNO     EMPNO     ENAME     SAL      HIREDATE
    ------------------------------------------------------------------------------------
    10             7521        Arun         1000    01-Jan-2005
                    7522       Bala           2000    01-Feb-2005 
                    7523        Chitra         3000    01-Mar-2005
    20             7524        Divya         4000    01-Apr-2005
                    7525        Elango        5000    01-May-2005 
    30             7526        Fathima      6000    01-Jun-2005
                    7527        Gopal         7000     01-Jul-2005
                    7528        Hari            8000    01-Aug-2005
    Published by: BluShadow on February 15, 2012 08:29
    addition of {noformat}
    {noformat} tags for improved (not perfect, due to OP's source) readability.  Please read {message:id=9360002} and learn to do this yourself.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

    According to my understanding, you could require something like this:

         DEPTNO      EMPNO ENAME             SAL HIREDATE
    ---------- ---------- ---------- ---------- ---------
            10       7934 MILLER           1300 23-JAN-82
                     7839 KING             5000 17-NOV-81
                     7782 CLARK            2450 09-JUN-81
            20       7876 ADAMS            6200 12-JAN-83
                     7369 SMITH             800 17-DEC-80
                     7788 SCOTT            3000 09-DEC-82
                     7566 JONES            2975 02-APR-81
                     7902 FORD             3000 03-DEC-81
            30       7844 TURNER           1500 08-SEP-81
                     7698 BLAKE            2850 01-MAY-81
                     7521 WARD             1250 22-FEB-81
                     7499 ALLEN            1600 20-FEB-81
                     7521 WARD             6500 22-FEB-81
            40       7900 JAMES            5900 03-DEC-81
                     7654 MARTIN           6500 28-SEP-81
    
    Then, the query you could use is:
    
    SELECT (CASE WHEN rn = 1 THEN deptno ELSE NULL END) Deptno, empno, ename, sal, hiredate
    FROM (
    SELECT deptno, ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY empno) rn ,empno, ename, sal, hiredate
    FROM emp); 
    

    Published by: claire on February 14, 2012 23:17

  • XML from the Oracle Table data

    Hi all

    I'm new to this network. I'm also new to oracle XML package. I want a help with the query below.

    CREATE TABLE EMP (ID NUMBER PRIMARY KEY, NAME VARCHAR2 (10), TELEPHONE NUMBER);

    INSERT INTO EMP (ID, NAME, PHONE) VALUES (11, 'Joy', 1234);
    INSERT INTO EMP (ID, NAME, PHONE) VALUES (22, 'Mike', 5678).
    INSERT INTO EMP (ID, NAME, PHONE) VALUES (33, "Jason", NULL);
    COMMIT;

    I want to export data from the EMP table in an XML file with the format below.

    Power required:

    <? XML version = "1.0" encoding = "UTF-8"? > < Joy STATICDATA > < EMP > < ID > 11 < /ID > < NAME > < / NAME > < / EMP > < / STATICDATA >
    <? XML version = "1.0" encoding = "UTF-8"? > < Mike STATICDATA > < EMP > < ID > 22 < /ID > < NAME > < / NAME > < / EMP > < / STATICDATA >
    <? XML version = "1.0" encoding = "UTF-8"? > < Jason STATICDATA > < EMP > < ID > 33 < /ID > < NAME > < / NAME > < / EMP > < / STATICDATA >

    I used some XML functions and you wrote the following query.

    Select XMLROOT (XMLELEMENT (staticdata, XMLELEMENT (EMP, XMLELEMENT(ID,ID), XMLELEMENT(NAME,NAME))), version "1.0" encoding = "UTF - 8') xml EMP;

    my query output:

    <? XML version = "1.0" encoding = "UTF-8"? >
    < STATICDATA >
    < EMP >
    < ID > 11 / < ID >
    Joy of < NAME > < / NAME >
    < / EMP >
    < / STATICDATA >
    <? XML version = "1.0" encoding = "UTF-8"? >
    < STATICDATA >
    < EMP >
    < ID > 22 / < ID >
    < NAME > Mike < / NAME >
    < / EMP >
    < / STATICDATA >
    <? XML version = "1.0" encoding = "UTF-8"? >
    < STATICDATA >
    < EMP >
    < ID > 33 / < ID >
    Jason < NAME > < / NAME >
    < / EMP >
    < / STATICDATA >


    But I want the out as the power required above. all records in a single line. can someone help me achieve the desired output. also can I export all columns of the table with something like select * from the table in the XML file?

    Thank you
    Delobelle

    Don't know why you need it on a single line, but you could:

    Select XMLTYPE (REGEXP_REPLACE (XMLROOT (XMLELEMENT (staticdata, XMLELEMENT (EMP, XMLELEMENT(ID,ID), XMLELEMENT(NAME,NAME))), version "1.0" encoding = "UTF - 8'), CHR (10) |)) ' *<><'))>
    FROM EMP;

    SY.

  • steps to create the new table using existing metadata

    Hi all


    That's what I want to achieve,

    I want to create a procedure that will take an input parameter as a table name lets say 'EMP ',.
    This procedure checks if this table exists or not?
    If they don't exist
    It will create a new table with the name _dummy join the existing table name (ex: new table will be EMP_DUMMY).
    and new table should have the same structure of the table, indexes, and constraints on the EMP table.


    NB: It should not be created using select as create table EMP_DUMMY select * from EMP where = condition false;



    Can someone help me please how to achieve this?

    Please, don't post duplicate discussions.

    Check your thread previous stored procedure to create a new table with the existing table structure to further discussions

  • How to load the child table by using a simple slider...?

    How can I loa the child table by using a PL/SQL block cursor...

    for example, I have a table of the source called method

    EmpNo, empname, deptid deptname

    and I have 2 tables of taget... EMP and Dept.

    EMP
    ===
    EmpNo, empname, deptid

    Dept
    ====
    deptID, deptname


    Here, I want to load method for emp data and... Dept where dept does not contain data in double...

    For now... I used the cursor and in the same cursor, I'm loading data first dept table and then to the emp table.
    I use for dept, exceptional setting, which will manage a PK constraint violation error by doing nothing...

    in this way, there will be no duplicates... but it is not advisable because I 15,00,000 lines in the method table.

    Is there another way to do it?

    OK, I'd probably go with the method two insert, but as an alternative, here's a way to do it in a single insert:

    create table emp_info as
    select 1 empno, 'a' empname, 101 deptid, 'aaa' deptname from dual union all
    select 2, 'b' empname, 201 deptid, 'bbb' deptname from dual union all
    select 3, 'c' empname, 101 deptid, 'aaa' deptname from dual union all
    select 4, 'd' empname, 101 deptid, 'aaa' deptname from dual union all
    select 5, 'e' empname, 301 deptid, 'ccc' deptname from dual;
    
    create table emp (empno number primary key, empname varchar2(3), deptid number);
    
    create table dept (deptid number primary key, deptname varchar2(3));
    
    insert all
      WHEN rn = 1 THEN
           into dept (deptid, deptname)
           values (deptid, deptname)
      WHEN rn > 0 THEN
           into emp (empno, empname, deptid)
           values (empno, empname, deptid)
    select empno, empname, deptid, deptname, rn
    from   (select empno,
                   empname,
                   deptid,
                   deptname,
                   row_number() over (partition by deptid order by empno) rn
            from   emp_info);
    
      8 rows inserted
    
    commit;
    
    select * from emp;
    
         EMPNO EMP     DEPTID
    ---------- --- ----------
             1 a          101
             3 c          101
             4 d          101
             2 b          201
             5 e          301
    
    select * from dept;
    
        DEPTID DEP
    ---------- ---
           101 aaa
           201 bbb
           301 ccc
    

    You must test these two methods to see which is more efficient.

Maybe you are looking for

  • stations radio streaming on my computer.

    I find no more mailing list radio stations on my IMac computer.  I think it was in iTunes, just a text list of all categories of music.  Where can I find it?

  • Satellite U400 - touch-pad scroll function does not work

    After installing Windows7, the scrolling feature no longer works. New driver are installed - doesn t workI tried to install the vista driver - t doesn't workFN + F9 are enabled. I put t know what could be the problem. Any ideas?Thank youNik

  • Boot from USB on Satellite Pro A100

    Hi, I own a satellite pro A100 computer laptop. I tested a live distribution of linux which is bootable from a usb key. When I set the bios to boot from removable devices with my inserted bootable usb key does not start it and comes back to my instal

  • HP LaserJet M251n: Printer HP M251n locked

    Hello I bought the HP LaserJet M251n color 200 printer about a year and now I don't remember the password I put for the web interface. As a result, I'm now locked out of the web interface, but also the screen of the printer. Could someone please help

  • Windows 10 and virtual Broadcom wireless adapter

    The app will win 10 tell me that my Broadcom Wireless Adapter virtual will no Windows 10 support and I am not able to connect to internet and I need to update the driver.  I have the latest driver which is version 5.60.48.18. It is a Dell Studio 1557