Implementation of selcet Top N in Oracle

Source: Internet
Author: User

I heard that SQL Server and MySQL have top keywords and can return the first N records of sorting records. For example, select top 10 * From table_name order by col_name. I think Oracle must have similar functions, oracle uses the rownum keyword instead of the top keyword.

To retrieve the Top N records from a query, you can use the following syntax:

Select *
From (your ordered query) alias_name
Where rownum <= rows_to_return
Order by rownum;

Write the following query statement according to the above Syntax:

Select * from (select rownum from gzl _ process flow status order by process instance number) kk where rownum <= 10 order by rownum;

Sort the gzl _ flow status table by process instance number and then return the first 10 rows. To return the last 10 rows, you only need to sort them in descending order. This is easy to think. Kk is the alias of A subquery.

Select title, art_id, reader_count
From (select title, art_id, reader_count
From article
Where (sys_cat <> 'new') and (eff_status = 'A ')
Order by reader_count DESC)
Where (rownum <= 3)
Order by rownum ASC

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.