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_TABLE
WHERE EMP_NUMBER=1000
FETCH FIRST n ROWS ONLY
 
 
Last N rows
 
 
SELECT EMP_NAME FROM EMP_TABLE
WHERE EMP_NUMBER=1000
ORDER BY EMP_NAME DESC
FETCH FIRST n ROWS ONLY