Rownum field of Oracle

Source: Internet
Author: User

The rownum field in Oracle is a strange field. Take a test table with 26 records as an example.
Select * from test where rownum> = 1;
Select * from test where rownum> = 2;
Select * from test where rownum <= 10;
26 records were found in the first SQL statement, 10 records were not found in the second SQL statement, and 10 records were found in the third SQL statement.


This is because rownum is a virtual field that is generated gradually when output is recorded.
For the first SQL statement, the rownum of the first record is 1, and the condition is output. Therefore, the rownum of the second record is 2, and the condition is output, all records are retrieved.
For the second SQL statement, the rownum of the first record is 1, which does not meet the condition and is not output. Therefore, the rownum of the second record is still 1. If the condition is not met, it is not output, none of the records were found.
For the third SQL statement, the rownum of the first record is 1, and the condition is output. Therefore, the rownum of the second record is increased to 2, and the condition is output, until the rownum of all the records after 11th and is changed to 11, the condition is not met and is not output.

Therefore, to query the records from the nth to the mth rows in the test table, we should write as follows:
// Filter the formed rownum
Select * from (
// Use a SELECT statement to enclose the SQL to be queried. At this time, rownum has been formed.
Select row _. *, rownum _ from (select * from test) Row _
) Where rownum _ <= m and rownum _> = N;

 

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.