Q
Problem solve Get help with specific problems with your technologies, process and projects.

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.


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 last published in May 2001

Dig Deeper on Oracle and SQL

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide.com

SearchDataCenter

SearchContentManagement

SearchHRSoftware

Close