The first 10 or 10th to 20th records that obtain query results in Oracle

Source: Internet
Author: User


The first 10 or 10th to 20th records ROWNUM obtained in Oracle are commonly used for paging output. for example, the SELECT name FROM member where rownum <= 10 statement is used to output the first 10 records, similar to the SQL sever top (select top 10 * from tablename ), however, rownum should be more powerful for the output of the specified number range: SELECT name FROM (SELECT name, ROWNUM rn FROM member) WHERE rn> = 10 AND rn <= 20 this statement outputs 10th to 20th records. Why should we use nested queries? Because rownum itself can only be compared using the <= method, only when it is converted to an instance can it be compared with> =. In actual use, www.2cto.com usually requires the latest records. Therefore, you need to sort the records before getting rownum <= Common SELECT * FROM (SELECT m. * FROM member m order by create_time DESC) where rownum <= 10, some may say SELECT m. * FROM member m where rownum <= 10 order by create_time DESC; the main difference between the two statements is: whether to sort first, query first, or then sort. Obviously, the first sentence is sorting first and then querying, and the second sentence is sorting first. The purpose of the two is to get the results of the desired class.
 

Related Article

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.