How to query the first few pieces of data in Oracle

Implementing select top N in Oracle:
Since Oracle does not support the select top statement, the combination of order by and rownum is often used in Oracle
to implement the query of select top n.
Simply put, 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 (the number of records extracted )
order by rownum asc

如:select id,name from (select id,name from student order by name) where rownum<=10 order by rownum asc

Sort by name and extract the top ten data

 

Attachment: The method of taking 100-150 pieces of data

1. Best option: use analytic functions

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

For example, if you want to take out 100-150 records, sort 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 virtual column

select tname,tabtype from (                    

      select tname,tabtype,rownum rn from tab where rownum <= 150                  

) where rn >= 100;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325379481&siteId=291194637