How to query the first few data records in Oracle

Source: Internet
Author: User

Implement select Top N in Oracle:
Since Oracle does not support the select top statement, order by and rownum are often used in Oracle.
To query the select Top N.
To put it simply, the implementation method is as follows:
Select column name 1... column name N from
(
Select column name 1... column name n
From table name order by column name 1
)
Where rownum <= N (number of Records extracted)
Order by rownum ASC

For example, select ID, name from (select ID, name from student order by name) Where rownum <= 10 order by rownum ASC

Sort the top 10 pieces of data by name

 

Appendix: Method for retrieving-data records

1. Best Choice:Using analysis functions

Row_number () over (partition by col1 order by col2)

For exampleRetrieve-records, sorted by tname
Select tname, tabtype from (

Select tname, tabtype, row_number () over (order by tname) Rn from Tab

) Where rn between 100 and 150;

2. Use rownum as the virtual Column

Select tname, tabtype from (

Select tname, tabtype, rownum rn from TabWhere rownum <= 150

) Where rn >=100;

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.