Typical paging code in Oracle

Source: Internet
Author: User

Typical paging code in Oracle

Since there is no top keyword in Oracle, the paging code in SQL Server does not apply to Oracle. How can paging be implemented in Oracle?

-- Query all data

STUNO STUNAME STUAGE STUID STUSEAAT
--------------------------------------------------------
9 Wang Wu 15 5.9876E + 15 5
13 Haha 15 5.9876E + 15 5
15 Li Si 12 1.5666E + 10 6
1 66 10 5566554666 5
1 66 10 5566554666 5
1 66 10 5566554666 5
1 66 10 5566554666 5
1 66 10 5566554666 5
1 66 10 5566554666 5
1 66 10 5566554666 5
1 66 10 5566554666 5
1 66 10 5566554666 5
1 66 10 5566554666 5

For example, I want to query the second to fourth records in the stuInfo table.

-- Two-layer nested Paging

SQL> -- two-layer nested Paging
SQL> select * from (select stuInfo. *, rownum as rn from stuInfo where rownum <= 4) where rn> = 2;

STUNO STUNAME STUAGE STUID STUSEAAT RN
------------------------------------------------------------------
13 Haha 15 5.9876E + 15 5
15 Li Si 12 1.5666E + 10 6
1 66 10 5566554666 5

-- If I do not have other sorting operations on the original table, two layers of nesting can meet the requirements, but if I have a condition that the student ID must be sorted in descending order first, what about the second to fourth records?

You must use layer-3 nesting.

SQL> select * from (select stu. *, rownum as rn from (select stuInfo. * from stuI
Nfo order by stuno desc) stu where rownum <= 4) where rn> = 2;

STUNO STUNAME STUAGE STUID STUSEAAT RN
------------------------------------------------------------------
15 Li Si 12 1.5666E + 10 6 2
13 Haha 15 5.9876E + 15 5 3
1 66 10 5566554666 5 4

This completes the paging query.

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.