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)
- 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:
- SELECT * FROM
- (SELECT * FROM CUSTOMER ORDER BY NAME)
- WHERE ROWNUM <= 3
- 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.