[SQL] Three Methods of Oracle paging Query

Source: Internet
Author: User
ArticleDirectory
    • Use pseudo-column rownum
    • Use analysis functions
    • Use minus
Use pseudo-column rownum

Query the first 10 records

 
Select * From t_user t where rownum <10;

Rank the first three records by student ID

 
Select * from (select ID, realname from t_user order by id asc) Where rownum <= 3

Paging SQL statement, which extracts 10 records from 10th records.

 
Select * from (select rownum RN, ID, realname from (select ID, realname from t_user) Where rownum <= 20) T2 where t2.rn> = 10;

Based on the student ID ranking, 10 records are extracted starting from 10th records.

 
Select * from (select rownum RN, ID, realname from (select ID, realname from t_user order by id asc) Where rownum <= 20) T2 where t2.rn> = 10;

[Note]
1. Where rownum> 1 cannot capture records.
2. Where rownum between 2 and 10 cannot capture records.

Use analysis functions

Use the analysis function row_number () over (order by field)

Rank the first three records by student ID

 
Select * from (select ID, realname, row_number () over (order by id asc) Rn from t_user) Where rn <= 3

Based on the student ID ranking, 10 records are extracted starting from 10th records.

 
Select * from (select ID, realname, row_number () over (order by id asc) Rn from t_user) Where rn between 10 and 20

Use minus

Extract 10 records from the first 10th records.

 
Select * From t_user where rownum <20 minus select * From t_user where rownum <10;

After sorting by ID, 10 records are extracted from the first 10th records.

(Select * from (select * From t_user order by id asc) Where rownum <20) minus (select * from (select * From t_user order by id asc) Where rownum <10 );

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.