Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2004
    Posts
    8

    Unanswered: Selecting first x rows of a table

    What is the simplest way to select the first x number of rows in a table?

    Thanks,

    Sean

  2. #2
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Cool

    SELECT * FROM MYTABLE WHERE ROWNUM <=X;

    PS: It all depends what you mean by FIRST rows.
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  3. #3
    Join Date
    Mar 2004
    Posts
    370
    But pay attention that it does not return the same x rows every time you execute it There is no x first rows in Relational model.You just may hava first x rows that have sorted in alphabetical order.
    -Best wishes

  4. #4
    Join Date
    Jan 2004
    Posts
    492
    The classic example is the TOP-N query from the EMP table:
    Code:
    select empno, sal
    from (select empno, sal
            from emp
            order by sal desc)
    where rownum <= 5 -- or however many top N you want grab
    As the others said just because you inserted rows in a certain order does not guarantee Oracle spits them back out at you in that same order. So use an order by if you need "first" rows.
    Oracle OCPI (Certified Practicing Idiot)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •