How to use ROWNUM in query

There are 1000 records in the employee table.I need to display 50 records each time by the order of the employees' names. In order to get the employee records from the 51st to the 100nd in the table, I tried to use ROWNUM.
However, the following query doesn't do the job:
select first_name, last_name from employee where rownum < 101 and rownum >49 order by last_name, first_name
I currently use the following query:
select * from (
(select first_name, last_name from employee where rownum < 101 order by last_name, first_name)
Minus
(select first_name, last_name from employee where rownum < 50 order by last_name, first_name )
) order by 2,1
The query works but is quite complictated. I would like to know if there a simpler way to do so.
Thanks in advance.
Helena

The generally preferred query is something along the lines of
SELECT *
  FROM ( SELECT a.*, rownum rn
           FROM (<<your query>>) a
          WHERE rownum <= <<MAX VALUE>>)
WHERE rn >= <<MIN VALUE>><<your query>> here would be
SELECT first_name, last_name
  FROM employee
ORDER BY first_name, last_nameNote that if you wanted to use the MINUS construct, you would generally need to move the rownum clause outside the query that is doing the ORDER BY. The query
SELECT first_name, last_name
  FROM employee
WHERE rownum < 101
ORDER BY first_name, last_namefetches the first 100 rows and then orders them, which is not generally what you want.
Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC

Similar Messages

  • How to use WMI to query to distinguish legacy and UEFI mode Windows?

    How to use WMI to query to distinguish legacy and UEFI mode Windows?

    This document covers requirements for UEFI support.
    http://msdn.microsoft.com/en-us/windows/hardware/gg463144.aspx
    One of the requirements seems to be ACPI. The Win32_BIOS class of WMI has a BIOSCharacteristics property that indicates if ACPI is supported. See this link for properties exposed by Win32_BIOS:
    http://msdn.microsoft.com/en-us/library/aa394077(v=VS.85).aspx
    There is a PowerShell example showing how to get this information at the bottom. You will need to study the document in the first link to see what else is required.
    Richard Mueller - MVP Directory Services

  • How to use one af:query for multiple VOs

    Hi all,
    How can we use an af:query component to query on multiple VOs?
    Think of a page with search area and a tabbed pannel with two tables based on different view objects.
    Any ideas?
    Thanks
    Version
    ADF Business Components 11.1.1.56.60
    Java(TM) Platform 1.6.0_18
    Oracle IDE 11.1.1.3.37.56.60
    Edited by: Barbara Gelabert on 28-dic-2010 6:55

    Hi Santosh,
    Not yet implemented, but I was thinking on overriding the query listener and creating a view critera on the second view object as a copy of the first one. That involves capturing operands, operators and values set and then applying the criteria and executing the query.
    Anyway, in case you don't need complex operators in the search area but can add them as where clauses to the query statement, you can use search forms instead of query components as Shay Shmeltzer suggests in [url http://blogs.oracle.com/shay/2010/12/combining_multiple_queries_and.html?goback=.gde_1002457_news_308820955]his post.
    Regards,
    Barbara

  • How to use this sql query in oracle?

    Hi all,
    i am using one sql query that is
    SELECT @ToDate = '2012-10-03 00:00:00.000'
    select @BetweenDate = DATEADD(MM,-1,@ToDate)
    select @FromDate = DATEADD(m,DATEDIFF(m,0,@BetweenDate),0)
    SELECT @ToDate = DATEADD(month, ((YEAR(@BetweenDate) - 1900) * 12) + MONTH(@BetweenDate), -1)
    so @todate value is = '2012-10-03 00:00:00.000'
    so in @betweendate value will come 1 month before like '2012-09-03 00:00:00.000'
    again in @fromdate value will come like that '2012-09-01 00:00:00.000' means first date of @betweendate
    and again @todate value will come like that '2012-09-30 00:00:00.000' means last date of @betweendate
    it's happening in sql and i have to use same logic in oracle also.
    how to use it??
    thanks

    declare
    todate date:= to_date('2012-10-03 00:00:00','yyyy-mm-dd hh:mi:ss');
    betwendate date := add_months(todate,-1);
    for datediff / additions you can direct subtract/add two different date variables
    like
    datediff = betweendate -todate
    dateadd := todate+1;

  • How to use dynamic select query which queries from 3 different table.

    Hi All,
    I am new to Toplink, i would like to use a named query to select some of the columns from 3 different tables with dynamic where clause.
    I have used the following lines. Please tell me how to get code for the dynamic where clause.
    First try :
    Vector objPersons = (Vector)session.executeQuery("findPersonByGlobalID",Person.class,vQueryArguments);
    The above method is not returning the vector or collection.
    Second Try:
    ReadAllQuery query = new ReadAllQuery(Person.class);
    query.useCollectionClass(LinkedList.class);
    LinkedList person = (LinkedList) session.executeQuery(query);
    The second try is returning the collection but, this fetches all the record from the table.
    1. How to query for range of records?
    2. How to query from multiple tables for some range. How to use dynamic range values ?
    Please reply with your answers or some pointers would help.
    Thanks and Regards,
    Vijay.B

    Hi,
    Did you try using a SQLCall ? It might be able to satisfy you requirements .. :-
    Employee employee = (Employee) session.executeSelectingCall(
        new SQLCall("SELECT * FROM EMPLOYEE WHERE EMP_ID = 44")
    );You can get more information here :-
    http://www.oracle.com/technology/products/ias/toplink/doc/1013/MAIN/_html/qrybas004.htm
    Regards,
    Sandeep

  • How to use "%CT" in Query Designer

    Hello everyone,
    i read that to calculate "Percentage Share of Result" in Query Designer we can use "%CT operator".
    But in my Query Designer (Version 7.10, SP10, Patch1) only "%" and "%A" are available to use. There ist no operator named "%CT".
    Can someone tell me how to use this operator?
    Thanks a lot and best regards,
    Susanne

    Hi,
    In the % Functions in a Formula you can find %, %A, %CT, %GT, %RT all 5 of them .
    My BW system is at Following Upgrade level:
    Software Component    Release    Level    Highest Support
    SAP_BW                701       0007            SAPKW70107
    If at a lower upgrade level, you need to upgrade till Specified BW Release to view these Fxn.
    You can check yours at System (Top of the window) --> Status . Click the Icon of magnifying glass named Componenet Info to view yours.
    Regards
    Raj Rai

  • How to use variant in Query broadcasting?

    Hi,gurus here,we need to distribute report based on query daily to users.
    We hope the required-entry variable of query can be filled according logic such as curren day,curren month,etc. automatically.
    In General Precaculation tab of Setting new setting we can see 2 methods of Variable Assignment:
    Determine here
    Determine from variants
    Apparently Determine here can just distribute query report based on fix value, it does not serve our purpose.
    But in Determine from Variants,we can see nothing from Variant list(Just the first select value which is No Selection Possible). We also tried to define a variants in analyzer,but we can not see the variant here.
    So how to use it to serve our purpose of distribute query report to users daily with automatic required-entry value filled?

    Hi,
    Please refer the article from the below link, it contains all the steps and will erase all your doubts -
    [http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/9075b11a-6e43-2c10-b7be-cfa0e97ec0cc?QuickLink=index&overridelayout=true]
    Hope this helps.
    Regards,
    RahulM

  • How to use Rownum?

    Hi everyone,
    Suppose I am having 100,000 records, and i want to filter only 50,000 records.
    How i can use rownum in OWB.
    I think in join i can use it, other that else any other way
    Regards
    Rachit

    tyr to use a view to reference the data and create rownum along with it.
    in 9.2 we dont have a true support to analytic functions. using a view would be my chocie.

  • How to use a CF Query in Flash

    I have a new project where I need to populate a scrolling
    Flash banner with titles from a ColdFusion query. Have experience
    in ColdFusion but somewhat limited in Flash. If I have a CF query
    on the page that contains the Flash, how can the results of the
    query of some 15 titles be passed into Flash and then looped
    through and displayed in Flash? Code examples of passing to Flash
    and looping through in Flash would be much appreciated. Using CF 8
    and Flash CS3.

    Here is a nice tutorial:
    Moving data from ColdFusion CFCs to Flex 2 applications
    http://www.adobe.com/devnet/flex/articles/helloworld.html

  • How to use rownum=2 in update statement  ?

    Hi all,
    we are migrating the data from mainframe to oracle. migration team extracted the data into flat file. we are using this flat file to load in oracle.Finacle have menu option to load data from flat file to corresponding oracle tables.whenever i given this flat file to Menu it will give error report like below if any rows have error.
    here "A/c. Opening Matrix" is the error.
    error report:-
    A/c. Opening Matrix
    The Above Error Was For record No: 1
    A/c. Opening Matrix
    The Above Error Was For record No: 2
    we are extracting and storing all the error message and corresponding record no in shell script as below.
    err_msg=`echo ${error_msg[i]}`
    rec_num=`echo ${error_msg[i]}| cut -d: -f2`
    we need to update the upload status either upload success or failure and error message in upload history table. iam writing the update statement as below.
    update statement:-
    update upld_hist set upld_status='ERR' ,upld_err='$err_msg' where rownum=$rec_num;
    This is statement is updating only for rownum=1. other than this it is not updating the table. Please suggest me how to update the rows based on rownum?
    Thanks,
    Venkat Vadlamudi

    868591 wrote:
    Hi all,
    we are migrating the data from mainframe to oracle. migration team extracted the data into flat file. we are using this flat file to load in oracle.Finacle have menu option to load data from flat file to corresponding oracle tables.whenever i given this flat file to Menu it will give error report like below if any rows have error.
    here "A/c. Opening Matrix" is the error.
    error report:-
    A/c. Opening Matrix
    The Above Error Was For record No: 1
    A/c. Opening Matrix
    The Above Error Was For record No: 2
    we are extracting and storing all the error message and corresponding record no in shell script as below.
    err_msg=`echo ${error_msg[i]}`
    rec_num=`echo ${error_msg[i]}| cut -d: -f2`
    we need to update the upload status either upload success or failure and error message in upload history table. iam writing the update statement as below.
    update statement:-
    update upld_hist set upld_status='ERR' ,upld_err='$err_msg' where rownum=$rec_num;
    This is statement is updating only for rownum=1. other than this it is not updating the table. Please suggest me how to update the rows based on rownum?
    Thanks,
    Venkat VadlamudiUse Analytic ROW_NUMBER() ..
    http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions156.htm#i86310

  • How to use Group in Query Print Layout

    Hi All,
    I have prepared a query for fetching all purchase details along with tax code, now I have ordered it on tax code and prepared a query Print Layout,
    Now within this Query Print Lay Out I want to group the tax code and want to add total of some columns at end of every tax code.
    Request to guide how to achieve this.
    Thanking you in advance,
    Samir Gandhi

    We need to use Sort and Group in combination.
    Select the repeatative area and in PLD menu select Sort, then select the column to be sort.
    then in Header / Footer of that respective repeatative area select the fields and put in the group no. in the General.

  • How to use RIDC to query WebCenter Content Tables

    Hi all
    Can we use RIDC API to query WebCenter Content 11g Tables.if yes then How can I achieve this?
    i am newbie

    always find in oracle doc
    http://docs.oracle.com/cd/E21764_01/doc.1111/e11011/c05_core.htm#BABBABBF
    and this too
    http://jonathanhult.com/blog/2012/07/ridc-versus-cis/

  • How to use RIDC to query WebCenter Content Tables/Views

    Hi,
    Can I use RIDC API to query WebCenter Content 11g Tables/Views, if so, How can I achieve this?
    Regards,

    Yes, you can.
    Call one of the Schema services (such as GET_SCHEMA_VIEW_VALUES) and you will get back a ResultSet which you can iterate over.
    http://docs.oracle.com/cd/E21764_01/doc.1111/e11011/c05_core.htm#BABBABBF
    Some links to RIDC code examples can be found here: http://jonathanhult.com/blog/2012/07/ridc-versus-cis/
    Jonathan
    http://jonathanhult.com

  • How to use ReportQuery to query against two types of objects?

    I have two loosely related classes Account and AccountState. They are not mapped to each other, have no inheritance or other relationships, but they both have account_id as their key attribute.
    I would like to build a ReportQuery to return selective attributes across Account and AccountState. Is there anyway to do so?
    Thanks in advance.
    George Zou

    George,
    Typically for the scenario you are describing I would recommend adding a 1:1 relationship from Account to state using the shared PK. If this is an option you should note that to keep the ID napped in each as a direct-to-field you will need to set-up the 1:1 using a target foreign key.
    For every 1:1 relationship in your model TopLink automatically creates a 1:1 query key. It is these query keys that you are implicitly using when defining expressions.
    If you really don't want to add the 1:1 mapping to your model then you can alternatively add a 1:1 query key yourself to your Account class that allows for the relationship to be used in an expression and thus a Report Query.
    Example 6-21 in the 9.0.4 application developers guide shows how to create a OneToOneQueryKey programmatically.
    I would guess your descriptor after-load method would look something like:
    public static void addToAccontDescriptor(Descriptor descriptor)
    OneToOneQueryKey ownerQueryKey = new OneToOneQueryKey();
    ownerQueryKey.setName("state");
    ownerQueryKey.setReferenceClass(AccountState.class);
    ExpressionBuilder builder = new ExpressionBuilder();
    ownerQueryKey.setJoinCriteria(builder.getField("ACCOUNT.ID").equal(builder.getParameter("ACCOUNT_STATE.ID")));
    descriptor.addQueryKey(ownerQueryKey);
    Now whether you added the 1:1 mapping or the 1:1 query key the ReportQuery would look the same:
    ReportQuery rq = new ReportQuery(Account.class, new ExpressionBuilder());
    rq.addAttribute("id");
    rq.addAttribute("State Value", rq.getExpressionBuilder().get("state").get("value"));
    I hope this helps,
    Doug

  • How to use Xpath to query elements loaded from different xml docs?

    I have a big DBLP XML doc (95mb), to load into the container, i splitted the doc into many small xml docs by the publication types, such as articles, books, inproceedings, etc.
    I tried to use xpath query such as
    query 'collection()/dblp[article/author]/book[title]'
    I got empty answers.
    but if i use XQuery such as
    query 'let $r:=collection()/dblp,$a :=$r/inproceedings/author
    for $l2 in $r/book
    where $a and $l2/title
    return <result> {$l2}{$a}</result>'
    i got answers. However, if i remove {$a} from the return clause, i still got empty answers.
    is there any problem with my Xpath query and flwor Xquery?
    regards,
    xiaoying

    Hi John,
    Following is the procedure i have used to load the xml docs (after the indexes have been created):
    private static void loadXmlFiles(File path2DbEnv,String theContainer, File file) throws Throwable {
         //Open a container in the db environment
              XmlManager theMgr = null;
              XmlContainer openedContainer = null;
              myDbEnv env;
              try {
              env = new myDbEnv(path2DbEnv);
         theMgr = new XmlManager(env.getEnvironment(), new XmlManagerConfig());
         //use node container
         theMgr.setDefaultContainerType(XmlContainer.NodeContainer);
                   try{
                   openedContainer = theMgr.createContainer(theContainer);
                   }catch (XmlException e){
                        //if the container alreay exist, then open it;     
                        openedContainer = theMgr.openContainer(theContainer);
              //Get an update context.
              XmlUpdateContext updateContext = theMgr.createUpdateContext();
                   String theFile = file.toString();
                   String docName=file.getName();
              //Get the input stream.
              XmlInputStream theStream = theMgr.createLocalFileInputStream(theFile);
              System.out.println("Adding " + theFile + " to container " +
                             theContainer);
         //     Do the actual put
              openedContainer.putDocument(docName, // The document's name
              theStream, // The actual document.
              updateContext, // The update context
         //     (required).
              null); // XmlDocumentConfig object
              System.out.println("done.");
              //XmlException extends DatabaseException, which in turn extends Exception.
              // Catching Exception catches them all.
              } catch (XmlException e){
                   System.err.println("Error loading files into container " + theContainer);
                   System.err.println(" Message: " + e.getMessage());
                   //In the event of an error, we abort the operation
                   // The database is left in the same state as it was in before
                   // we started this operation.
                   throw e;
              finally {
              cleanup(theMgr, openedContainer);
    Unfortunately, i waited for around 20 hours 148mb doc was not loaded; so i gave up.
    Could you please give me some suggestions on how to load the large doc efficiently ? thanks.
    regards,
    xiaoying

Maybe you are looking for