Suppose a file in AS/400 DB2 has 1000 records. How can I retrieve the last 100 rows from the file?
By submitting your personal information, you agree that TechTarget and its partners may contact you regarding relevant content, products and special offers.
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
- Dozens more answers to tough SQL questions from Rudy Limeback.
- The Best SQL Web Links: tips, tutorials, scripts, and more.
- Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
- Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
- Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.
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.