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

Last N rows in DB2

Suppose a file in AS/400 DB2 has 1000 records. How can I retrieve the last 100 rows from the file?

This is an interesting twist on the usual "Top N" query question. There are two parts to the answer. First, you can get the last N simply by running a Top N query which includes an ORDER BY clause with the DESCENDING keyword.

Second, since DB2 is one of the databases which has implemented special syntax just for the purpose of this type of query (others being Microsoft SQL Server and Access with TOP and MySQL and PostgreSQL with LIMIT), you simply utilize that syntax. According to the DB2 Universal Database for iSeries SQL Reference page for the select-statement, this would be

select foo, bar
  from yourAS400table
order by foo descending
fetch first 100 rows only

For More Information


This was last published in October 2002

Dig Deeper on Oracle and SQL

PRO+

Content

Find more PRO+ content and other member only offers, here.

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.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close