Page 1 of 1

Fectch Rows

PostPosted: Sat Apr 03, 2010 10:47 am
by pratik397
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.

ThankYou

Re: Fectch Rows

PostPosted: Sat Apr 03, 2010 10:55 am
by dick scherrer
Hello,

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

Re: Fectch Rows

PostPosted: Sun Apr 25, 2010 11:18 am
by Anuj Dhawan
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.