What are the steps for implementing select top n in Oracle?

Source: Internet
Author: User

The following article mainly introduces how to implement the select top n operation in Oracle. I saw the actual operations on the Oracle SELECT TOP N on the relevant website two days ago, I think it's good. I just want to share it with you.

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 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... column name n)

Where rownum <= N number of extracted Records)

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

In Oracle's implementation of select top n, We need to extract the SQL statements of the first three customers 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:

 
 
  1. ID NAME  
  2. 08 eighth  
  3. 05 fifth  
  4. 01 first   

The above content is an introduction to the method for implementing the select top n in Oracle. I hope you will have some gains.

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.