Brief Analysis of Oracle Rownum

Source: Internet
Author: User

During application development, it is common to sort data according to certain rules and then retrieve the first few data records. In Oracle, order by is commonly used to obtain data smaller than Oracle Rownum. However, if you are not familiar with Oracle, you may find that the value retrieved from the SQL statement you wrote is incorrect. Why.

Oracle first Retrieves all the data and sorts the data in the sorting segment. Assume that you have an SQL statement as follows:

 
 
  1. SELECTEMP_NOFROMEMPWHEREROWNUM < 10ORDER BYEMP_NO 

During retrieval, the data is first retrieved, that is, the EMP table data is retrieved, and then the Order by operation is performed. Since Oracle Rownum data is retrieved before sorting, Oracle Rownum cannot be used to obtain the first 10 operations after sorting. So how should we operate it. In fact, it is very easy to use subqueries, first sort and then get Oracle Rownum. As follows:

 
 
  1. SELECTEMP_NOFROM(  
  2. SELECTEMP_NOFROM EMPORDER BYEMP_NO  
  3. )  
  4. WHERE  
  5. ROWNUM < 10 
  1. Abstract Oracle Check Constraints
  2. Introduction to Oracle index creation policies
  3. Oracle Where clause research conclusion
  4. Oracle UNION ALL
  5. Overview of Oracle Unique constraints

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.