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:
- SELECT * FROM
- (SELECT * FROM CUSTOMER ORDER BY NAME)
- WHERE ROWNUM <= 3
- ORDER BY ROWNUM ASC
Output result:
- ID NAME
- 08 eighth
- 05 fifth
- 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.