How will fetch last 5 rows from table in db2

Answers were Sorted based on User's Feedback



How will fetch last 5 rows from table in db2..

Answer / sree

SELECT * FROM TABLENAME ORDER BY PRIMARYCOLUMNNAME DESC
FETCH FIRST 5 ROWS ONLY ;

THIS ANSWER IS VALID ONLY IF THERE IS A PRIMARY KEY
COLUMN,OTHERWISE IT WONT WORK

IF THERE IS NO PRIMARY KEY COLUMN IN GIVEN TABLE ,I DON'T
KNOW THE ANSWER

FOR FETCHING FIRST 5 ROWS

SELECT * FROM TABLENAME FETCH FIRST 5 ROWS ONLY;

Is This Answer Correct ?    8 Yes 1 No

How will fetch last 5 rows from table in db2..

Answer / vaneesh khurana

Hi Harsha,

There is nothing to do with multiple of 5.
Simply declare the array of occours of 5 keep on filling
it when 6th record comes start over writing it from 1st.

At last we only left with the last 5 fetched rows.

Vaneesh Khurana

Is This Answer Correct ?    1 Yes 0 No

How will fetch last 5 rows from table in db2..

Answer / vaneeshh khurana

See there is no direct method to do so, but still if we want
last 5 rows only we can create a logic which is as follows.

1. Declare an array to save the fetch rows with occurs of 5.
2. Now fetch the rows and start saving in that array such
that when the count becomes 5 overwrite the 6th fetch in the
first occurs similarly the 7th fetch on the 2nd occur.
3. Now when all the fetching is done the array will contains
the last 5 fetched rows.

Is This Answer Correct ?    2 Yes 2 No

How will fetch last 5 rows from table in db2..

Answer / harsha

Vaneeshh, your logic will not work if number of records are
not in a multiple of 5.

Is This Answer Correct ?    1 Yes 1 No

How will fetch last 5 rows from table in db2..

Answer / mahyn

select * from tablename
order by primary_key desc
fetch first 5 rows only
with ur;

can be one way...

Is This Answer Correct ?    0 Yes 0 No

How will fetch last 5 rows from table in db2..

Answer / mayur bhayani

SELECT * FROM (SELECT * FROM T ORDER BY pk DESC FETCH FIRST 5 ROWS) AS X
ORDER BY PK;

Is This Answer Correct ?    3 Yes 11 No

Post New Answer

More DB2 Interview Questions

I have a subprogram B calling from mainprogram A.I have opened a cursor in A ,Can i fetch the same cursor in subprogram B , If yes , Please explain the reason?

1 Answers   HeadStrong,


What do you do to keep the cursor from closing after a commit?

1 Answers  


Why do we bind in db2?

0 Answers  


Which DB2 catalog tables are used to produce a list of table column by table for all tables in a database?

1 Answers  


If the base table underlying a view is restructured, eg. attributes are added, does the application code accessing the view need to be redone?

2 Answers  






how to execute qcmdexc in rpgle?plz write code also?

1 Answers   IBM,


how do you resolve -818 error in db2. where we have to see time stamp token. i said by seeing in load module and plan .is it correct or not. give clear explination for how to see time stamp token

2 Answers   IBM, TRD,


if i made sme changes in sub pgm wht r the things to be done for this ?

1 Answers   TCS,


What is dbrm? When it will be created?

0 Answers  


What happens to a tablespace when its recovery infromation has been removed and a full recovery is no longer possible?

2 Answers  


What is the picture clause of null indicator variable?

0 Answers  


In cursor program perform para varying until 1 by 1 fetch para cursor------close para in the blank what will come for confirming in the database?

1 Answers   HCL,


Categories