We have moved to www.dataGenX.net, Keep Learning with us.

Friday, October 26, 2012

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 retrieved 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




njoy the simplicity.......
Atul Singh

No comments :

Post a Comment