Fectch Rows

IBM's flagship relational database management system

Fectch Rows

Postby pratik397 » Sat Apr 03, 2010 10:47 am

I tried one querry to fetch first 5 rows from the Table As:

select * from table fetch first 5 rows only.

It fetches first 5 rows from the Table.

Similarly How to fetch last 10 rows ? I tried using "last" keyword in the above querry instead of "first" but its not working.
Need Help.

Posts: 5
Joined: Thu Mar 11, 2010 1:17 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Fectch Rows

Postby dick scherrer » Sat Apr 03, 2010 10:55 am


It fetches first 5 rows from the Table.
Well, it fetches 5 anyway. . . There is actually no such thing as the first 5 or last 10 rows in a table. . . First and last are more appropriately used with sequential files which have some physical order (even if not sorted).

The query returned the first 5 rows of the set of selected rows.

Of what value would be the "last 10" rows of an unspecified set?

The usual way to determine "first" and "last"is by specifying an order. If you order by descending and then fetch the first 5 rows of that set, you will get the "last 5".
Hope this helps,
User avatar
dick scherrer
Global moderator
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: Fectch Rows

Postby Anuj Dhawan » Sun Apr 25, 2010 11:18 am

Without an "order by" you can't rely on the result of the query you posted - it will give just any five-rows, as Dick has said. The ORDER BY clause is required to sort the results in the right order. If it is removed from the query, the results will still contain the top five, but they may be in no particular order. Just for a note, "Fetch n rows" is available since DB2 Version 7.
Anuj Dhawan
Posts: 273
Joined: Mon Feb 25, 2008 3:53 am
Location: Mumbai, India
Has thanked: 6 times
Been thanked: 4 times

Return to DB2


  • Related topics
    Last post