Which of the following is the best method to implement select top n in Oracle?

Source: Internet
Author: User

The following article describes how to implement the select top n operation in Oracle. We use the following example to illustrate how to implement the select top n operation in Oracle, the following is a description of the specific content of the article.

1. 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 statement.

To put it simply, the method for implementing select top n in Oracle 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... column name n)

Where rownum <= N number of extracted Records)

 
 
  1. ORDER BY ROWNUM ASC 

The following is a simple example.

The customer (id, name) Table has the following data:

ID NAME

01 first

02 Second

03 third

04 forth

05 th

06 sixth

07 seventh

08 eighth

09 ninth

10 tenth

11 last

The SQL statements of the first three customers are extracted by NAME as follows:

 
 
  1. SELECT * FROM  
  2. (SELECT * FROM CUSTOMER ORDER BY NAME)  
  3. WHERE ROWNUM <= 3  
  4. ORDER BY ROWNUM ASC  

Output result:

ID NAME

08 eighth

05 th

01 first

The above content describes how to implement the select top n method in Oracle, hoping to help you in this regard.

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.