Minimum hiredate in emp table

Hi,
There is an emp table, which a column hiredate(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 any other way to do it, without using subquery?
Regards,
Ritesh

Centinul:
Are you sure?
I added another record with the "lowest" hiredate. The OP's original version:
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 union all
  5      select 'D', 4, '1-Jan-2002' from dual)
  6  select * from t
  7  where hiredate = (select min(hiredate) from t);
E       ENUM HIREDATE
B          2 1-Jan-2002
D          4 1-Jan-2002Your version:
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 union all
  5      select 'D', 4, '1-Jan-2002' from dual)
  6  SELECT  MIN(ENUM)       KEEP (DENSE_RANK FIRST ORDER BY HIREDATE)       AS ENUM,
  7          MIN(HIREDATE)   KEEP (DENSE_RANK FIRST ORDER BY HIREDATE)       AS HIREDATE
  8  FROM    t;
      ENUM HIREDATE
         2 1-Jan-2002Your version also misses the ename column which the OP has, so yours needs to be more like:
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 union all
  5      select 'D', 4, '1-Jan-2002' from dual)
  6  SELECT  ename, MIN(ENUM) KEEP (DENSE_RANK FIRST ORDER BY HIREDATE) AS ENUM,
  7          MIN(HIREDATE) KEEP (DENSE_RANK FIRST ORDER BY HIREDATE) AS HIREDATE
  8  FROM    t
  9  GROUP BY ename;
E       ENUM HIREDATE
A          1 1-Oct-2008
B          2 1-Jan-2002
C          3 1-Mar-2008
D          4 1-Jan-2002Which kind of defeats the purpose :-)
Top replicate the behaviour of the OP's query:
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 union all
  5      select 'D', 4, '1-Jan-2002' from dual)
  6  select *
  7  from (SELECT t.*, DENSE_RANK() OVER(ORDER BY hiredate) rn
  8        FROM t)
  9  where rn = 1
E       ENUM HIREDATE           RN
B          2 1-Jan-2002          1
D          4 1-Jan-2002          1It still has a sub-query, but it is only one pass through the table which I took to be the point.
John

Similar Messages

  • How to find the greatest hiredate in emp table

    hi,
    how can I find out whose the latest recruit in the demo emp
    table.
    thanx.
    Sreekant

    Hello,
    Use following query.
    select * from emp
    where hiredate = (select max(hiredate) from emp);
    Adi

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

    Hi I'm new to oracle can anyone please help me in writing a program so that i can insert test data into emp table

    Hi,
    user11202607 wrote:
    thanks sanjay , frank . But how can i insert only 4 deptno's randomly and how can i insert only 10 managers randomly ,
    Sorry to pull Your legs and thanks for bearing my question. I want to insert into emp table where it has the empno, ename, sal, job, hiredate, mgr and deptnoThis should give you some ideas:
    INSERT INTO emp (empno, ename, sal, job, hiredate, mgr, deptno)
    SELECT  LEVEL                         -- empno
    ,     dbms_random.string ('U', 4)          -- ename
    ,     ROUND ( dbms_random.value (100, 5000)
               , -2
               )                         -- sal
    ,     CASE 
               WHEN  LEVEL =  1              THEN  'PRESIDENT'
               WHEN  LEVEL <= 4            THEN  'MANAGER'     -- Change to 11 after testing
               WHEN  dbms_random.value < .5  THEN  'ANALYST'
               WHEN  dbms_random.value < .5  THEN  'CLERK'
                                                 ELSE  'SALESMAN'
         END                         -- job
    ,     TRUNC ( SYSDATE
               - dbms_random.value (0, 3650)
               )                         -- hiredate
    ,     CASE
             WHEN  LEVEL > 1
             THEN  TRUNC (dbms_random.value (1, LEVEL))
         END                         -- mgr
    ,     TRUNC (dbms_random.value (1, 5))     -- deptno
    FROM     dual
    CONNECT BY     LEVEL <= 10                         -- Change to 10000 after testing
    ;The interesting part (to me, at least) is mgr. What I've done above is guarantee that the mgr-empno relationship reflects a tree, with the 'PRESIDENT' at its sole root. The tree can be any number of levels deep.
    Sample results:
    EMPNO ENAME        SAL JOB        HIREDATE  MGR DEPTNO
        1 GDMT        2800 PRESIDENT  30-AUG-04          2
        2 CVQX         400 MANAGER    24-MAY-06   1      2
        3 QXJD        1300 MANAGER    17-JUN-05   1      4
        4 LWCK        4800 MANAGER    15-JUN-06   2      2
        5 VDKI        3700 CLERK      08-SEP-01   4      2
        6 FKZS        2600 CLERK      18-DEC-06   4      1
        7 SAKB         700 ANALYST    30-JUN-00   5      4
        8 DVYY         300 ANALYST    22-SEP-01   2      1
        9 CLEO        2700 ANALYST    27-MAY-08   5      4
       10 RDVQ        3400 ANALYST    14-DEC-08   5      4For details on the built-in packages (such as dbms_random) see the [Parckages and Types manual|http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28419/d_random.htm#i998925].

  • How to get the select * from emp table output on the console  using java

    public class software {
          * @param args
         static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
         static final String DATABASE_URL = "jdbc:oracle:abc";
         private static Connection connection;
         private static Statement statement;
         public static void main(String[] args) {
              // TODO Auto-generated method stub
         try {
              System.out.println("-------THIS IS THE Class.forNameJDBC_DRIVER");
                   Class.forName(JDBC_DRIVER);
                   System.out.println("THIS IS THE Class.forNameJDBC_DRIVER");
                   connection = DriverManager.getConnection(DATABASE_URL, "abc",
                   "abc");
                   System.out.println("THIS IS THE connection abc ,abc");
                   statement = connection.createStatement();
                   //Query to find the values in the EMP table.
                   ResultSet resultSet = statement.executeQuery("SELECT * from EMP");
                   if(resultSet.next()){
                   System.out.println("THESE ARE THE VALUES IN EMP TABLE:"+resultSet);  /// How can i get all the values record wise on the  console ??????
                   resultSet.close();
         catch (ClassNotFoundException classNotFound) {
                   System.out.println("Driver not Found"+classNotFound.getMessage());
              } catch (SQLException sqlException) {
                   System.out.println("SQL Exception - bad sql");
                   System.out.println(sqlException.getMessage());
    }

    1sai
    Please assign the dukes here and in your previous threads.
    See [http://wikis.sun.com/display/SunForums/Duke+Stars+Program+-+How+it+Works]
    You are currently the all time career leader in un-awarded dukes, with including this thread 75 unawarded dukes for questions you marked as answered.
    It's even worse when you look and see that you have awarded many dukes as well. So you do know how to do it. You're just too lazy and rude to be bothered.
    Don't be a lazy wank.

  • How to display required data from emp table?

    Hi every one, this is my first post in this portal. I want display the details of emp table.. for that I am using this SQL statement.
    select * from emp where mgr=nvl(:mgr,mgr);
    when I give the input as 7698 it is displaying the corresponding records... and also when I won't give any input then it is displaying all the records except the mgr with null values.
    1)I want to display all the records when I won't give any input including nulls
    2)I want to display all the records who's mgr is null
    Is there any way to incorporate to include all these in a single query..

    Hi,
    937440 wrote:
    Hi every one, this is my first post in this portal. Welcome to the forum!
    Be sure to read the forum FAQ {message:id=9360002}
    I want display the details of emp table.. for that I am using this SQL statement.
    select * from emp where mgr=nvl(:mgr,mgr);
    when I give the input as 7698 it is displaying the corresponding records... and also when I won't give any input then it is displaying all the records except the mgr with null values.
    1)I want to display all the records when I won't give any input including nulls
    2)I want to display all the records who's mgr is null
    Is there any way to incorporate to include all these in a single query..It's a little unclear what you're asking.
    The following query always includes rows where mgr is NULL, and when the bind variable :mgr is NULL, it displays all rows:
    SELECT  *
    FROM     emp
    WHERE     LNNVL (mgr != :mgr)
    ;That is, when :mgr = 7698, it displays 6 rows, and when :mgr is NULL it displays 14 rows (assuming you're using the Oracle-supplied scott.emp table).
    The following query includes rows where mgr is NULL only when the bind variable :mgr is NULL, in which case it displays all rows:
    SELECT     *
    FROM     emp
    WHERE     :mgr     = mgr
    OR       :mgr       IS NULL
    ;When :mgr = 7698, this displays 5 rows, and when :mgr is NULL it displays 14 rows.
    The following query includes rows where mgr is NULL only when the bind variab;e :mgr is NULL, in which case it displays only the rows where mgr is NULL. That is, it treats NULL as a value:
    SELECT     *
    FROM     emp
    WHERE     DECODE ( mgr
                , :mgr, 'OK'
                )     = 'OK'
    ;When :mgr = 7698, this displays 5 rows, and when :mgr is NULL, it displays 1 row.

  • How to Model Emp table

    Hi Experts,
    I am new to obiee,i just want to model the normal emp Table in scott schema,
    Could You please explain me how to configure only Emp table like this
    and report should be like
    Manager Name Promt
    Table should be like
    Mangaer Name,Avg salary of employee under the manager
    Edited by: user1005945 on Feb 6, 2011 12:40 PM

    Hi Experts,
    I am new to obiee,i just want to model the normal emp Table in scott schema,
    Could You please explain me how to configure only Emp table like this
    and report should be like
    Manager Name Promt
    Table should be like
    Mangaer Name,Avg salary of employee under the manager
    Edited by: user1005945 on Feb 6, 2011 12:40 PM

  • Import only the rows of emp table from Test into emp table of Prod

    hi,
    Test Instance:
    =========
    I have a table "emp". I want to take export of it.
    Prod Instance:
    =========
    Table with name "emp" already exists.
    I want to import only the rows of emp table from Test Instance into emp table of Prod Instance.
    regards,
    charan

    Charan,
    Set the import command IGNORE=Y
    Importing into Existing Tables
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14215/exp_imp.htm#i1023662
    Regards,
    Hussein

  • Automatic LOV refresh Dept drop down on EMP table

    I have a editable DEPT table and editable EMP table on the same page. Each table have their own corresponding insert and delete buttons. There is a common commit and rollback button for the both tables.
    In my EMP VO I have a view accessor linked to the DEPT VO . The dept column in the EMP table on the page has a drop down list. I'm trying to get the LOV drop down to refresh on the EMP table after an insert/delete and subsequent commit on the DEPT table. Has any body had the same requirement ? I tried the partial triggers between the commit button and the LOV on the EMP table and no luck ! :-(
    thanks in advance

    Just discovered that the my LOV was pointing to the wrong VO. I had 2 VO for the DEPT table. Re-pointed the LOV to the same VO as the DEPT on the corresponding page. it's all solved !

  • Select query on emp table

    Hi,
    How to find display the o/p like manager name under dependent employess same like parent child relation ship on noraml emp table:
    sample o/p:
    name job
    xx manger
    yy sales
    yy1 sales
    aa manager
    rr marketing
    rr1 marketing
    Thanks

    921306 wrote:
    so like this i need parent child relationship between the manager under employees....So join the parent entity with the child entity to create the parent-child relationship.
    As both entities are in the same table, this means joining the table to itself. E.g.
    SQL> select
      2          child.ename                     as CHILD,
      3          child.job                       as CHILD_JOB,
      4          nvl(parent.ename,'<none>')      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 <none>
    14 rows selected.
    SQL> The basic concept is easy - define the parent entity, define the child entity, join the parent to the child.
    If some entities will not have parents (root entities), then an outer join is needed from the child entity to the parent entity.
    Simply because both entities are in the same table, does not change the logic or approach. It is perfectly valid to self-join a table.

  • Fetching ename who are all having max(sal) in each of job from emp table

    hi all,
    i need a query to fetch all the ename who are all having maximum salary in each job category from emp table
    thankx in advance
    regrds,
    punith

    SQL> select ename,job,sal
      2  from emp
      3  order by job,sal desc;
    ENAME      JOB              SAL
    SCOTT      ANALYST         3000
    FORD       ANALYST         3000
    MILLER     CLERK           1300
    ADAMS      CLERK           1100
    JAMES      CLERK            950
    SMITH      CLERK            800
    JONES      MANAGER         2975
    BLAKE      MANAGER         2850
    CLARK      MANAGER         2450
    KING       PRESIDENT       5000
    ALLEN      SALESMAN        1600
    TURNER     SALESMAN        1500
    WARD       SALESMAN        1250
    MARTIN     SALESMAN        1250
    14 rows selected.
    SQL> select ename,job,sal from emp
      2  where sal in(select distinct max(sal) from emp
      3* group by job)
    SQL> /
    ENAME      JOB              SAL
    FORD       ANALYST         3000
    SCOTT      ANALYST         3000
    MILLER     CLERK           1300
    JONES      MANAGER         2975
    KING       PRESIDENT       5000
    ALLEN      SALESMAN        1600
    6 rows selected.
    SQL>Is this what you need ?

  • Need demo of a simple chart item using emp table sal column

    DB version:10g
    Forms version 10g
    Hi folks,
    Can anyone please post some sample code for a simple chart item based on emp table in forms 10g.
    lets say show the salary variations using chart item.
    Please do help.
    thanks in advance.

    There are samples for doing this (not easy to find for 10g these days).
    http://www.oracle.com/technetwork/developer-tools/forms/news-section-086804.html
    Have a look for the BI Graph Patch link for 10g on this page.
    Steve

  • Find EMP tables without SAL column

    We have EMP tables in several of our schemas. Some of these EMP tables have SAL column missing.
    How can i find the schemas whose EMP tables have a missing SAL column from DBA_TAB_COLS view?

    Hi,
    Here's another way, that only requires one pass throught the view:
    SELECT       owner
    FROM       dba_tab_cols
    WHERE       table_name     = 'EMP'
    GROUP BY  owner
    HAVING       COUNT (CASE WHEN column_name = 'SAL' THEN 1 END)     = 0
    ORDER BY  owner
    ;This might be more efficient, but, unless you have thousands of tables called EMP, you won't notice it. You might keep this tecnique in mind for similar problems where efficency is imporatant.

  • Hierarchy count of emp table by job title

    Consider Emp table.
    I want the hierarchical count by job title wise.
    For president the count should be the total number of subordinates, i.e. the total number of emp's - 1 (the 1 is the president himself).
    Like that any manager, the count is the total number of subordinates.
    The output should be look like this:
    Job Subordinates
    President xx
    Manager xx

    I'm not sure what version you are looking into, but hierarchical total example is in 10g documentation (SQL Reference):
    <quote>The following example uses a GROUP BY clause to return the total salary of each employee in department 110 and all employees below that employee in the hierarchy:
    SELECT name, SUM(salary) "Total_Salary" FROM (
    SELECT CONNECT_BY_ROOT last_name as name, Salary
    FROM employees
    WHERE department_id = 110
    CONNECT BY PRIOR employee_id = manager_id)
    GROUP BY name;
    NAME Total_Salary
    Gietz 8300
    Higgins 20300
    King 20300
    Kochhar 20300
    </quote>

  • Error when trying to issue a grant statement for Scott's emp table

    I'm trying to complete a lab assignment in which we have to grant the select on Scott's emp table. Here is the statement I used. I'm wondering why did i receive the error that the table doesn't exist.
    SQL> GRANT SELECT ON SCOTT.EMP TO DEV;
    GRANT SELECT ON SCOTT.EMP TO DEV
    ERROR at line 1:
    ORA-00942: table or view does not exist
    Edited by: user566571 on Dec 4, 2009 8:19 AM

    Are you the owner of the emp table ? If not then
    Do the user(which you are using to grant the privilege) has GRANT ANY PRIVILEGE or GRANT ANY OBJECT PRIVILEGE privileges ?
    Do the user has been granted the appropriate privileges on the SCOTT.EMP WITH GRANT option ?
    To grant the privileges to the role, first your user should have those privileges and that with the option to grant to others using WITH GRANT.

  • Getting cummulative sum of salaries of employees from emp table

    Hi gurus,
    Can any body tell me how to get cummulative sum of salaries of employees in emp table by using analytical functions.
    thanks in advance....

    SeánMacGC wrote:
    Hello,
    Try:
    SELECT empno,
    SUM(SAL) OVER () cumulative_sal
    FROM emp;
    You need to order by to get the cumulative salary
    SQL> SELECT empno,
      2     SUM(SAL) OVER () cumulative_sal
      3    FROM emp;
         EMPNO CUMULATIVE_SAL
          7369          29025
          7499          29025
          7521          29025
          7566          29025
          7654          29025
          7698          29025
          7782          29025
          7788          29025
          7839          29025
          7844          29025
          7876          29025
         EMPNO CUMULATIVE_SAL
          7900          29025
          7902          29025
          7934          29025
    14 rows selected.
    SQL> SELECT empno, sal,
      2     SUM(SAL) OVER (Order by empno) cumulative_sal
      3    FROM emp;
         EMPNO        SAL CUMULATIVE_SAL
          7369        800            800
          7499       1600           2400
          7521       1250           3650
          7566       2975           6625
          7654       1250           7875
          7698       2850          10725
          7782       2450          13175
          7788       3000          16175
          7839       5000          21175
          7844       1500          22675
          7876       1100          23775
         EMPNO        SAL CUMULATIVE_SAL
          7900        950          24725
          7902       3000          27725
          7934       1300          29025
    14 rows selected.

Maybe you are looking for

  • Rating / Label "Buttons" active, although not shown in Grid View

    While I was investigating the reason for JPEG images being written back with no obvious changes applied to them (see also How to filter on Metadata Status "Has been changed"?) I noticed something I think is a bug: Becaus it sometimes happened to me t

  • Loading content from multiple MiniDV tapes to one DVD

    I am being offered this "One Touch" feature on iDVD to copy my Camera footage to a DVD. I am wondering what the best process is to follow if I wanted to, say, merge :30 minutes of content from one MiniDV and :20 minutes of content from another. Shoul

  • Fan control - wich package?

    Hello *! a small question to the more knowledgable around: in wich package is the software bit that controls the fan? I try to construct a system starting from the reduced/core system group and that functionality doesn't seem to be in this two groups

  • Problem: Overcropping in PSE

    For about a week now I have experienced a persistent problem with the crop tool in Photoshop Elements:  Now matter how little I crop, the image is reduced to nothing more than a dot. Has anybody else encountered this problem - and found a solution? O

  • Why do I need to be signed into my Adobe ID to use Elements Editor?

    Everytime I use Elements Editor I am automatically logged onto Adobe.   Why is this when I always thought Elemens was a Stand Alone piece of software?