How to do SQL select top N ... in AS400

James Moore picture James Moore · May 17, 2010 · Viewed 75.3k times · Source

How do you perform a

Select top N * from as400table

type query against an as400/db2 database

Answer

James Moore picture James Moore · May 17, 2010
Select col1,col2
from 
as400table
where col1='filter'
order by col1
fetch first N row only

Remember to set an ORDER BY clause, because DB2 does not guarantee that the rows returned by FETCH FIRST N ROW ONLY are always the same N.