Returning a specified number of rows

Returning a specified number of rows

How do I write a query that can return a specific number of rows? Say, base on the constant that I set, I can either return 20 rows, 30 rows from a table (or multiple tables)? Thanks.


    Requires Free Membership to View

    By submitting your registration information to SearchOracle.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchOracle.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

If you're using SQL/Server or MS Access, use the TOP keyword --

select top 20 foo, bar
  from thetable

If you're using MySQL, use the LIMIT keyword --

select foo, bar
  from thetable
 limit 20

If you're using Oracle, use ROWNUM or RANK() as explained in "Performing top-N queries in Oracle".

For More Information


This was first published in May 2001

Join the conversationComment

Share
Comments

    Results

    Contribute to the conversation

    All fields are required. Comments will appear at the bottom of the article.