DB2 query to select first or last N rows
DB2 query to select first or last N rows
There may be instances when you wish to select first or last N rows.
You can use the following query to limit the number of rows retreived
using select command.
First N rows
SELECT EMP_NAME FROM EMP_TABLEWHERE EMP_NUMBER=1000FETCH FIRST n ROWS ONLY
Last N rows
SELECT EMP_NAME FROM EMP_TABLEWHERE EMP_NUMBER=1000ORDER BY EMP_NAME DESCFETCH FIRST n ROWS ONLY