Search code examples
sqlibm-midrange

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


How do you perform a

Select top N * from as400table

type query against an as400/db2 database


Solution

  • 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.