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

DB2 is a A) data base/data communication system B) data base C) RDBMS D) Programming language

4 Answers   Accenture,


What do you mean by NOT NULL? When will you use it?

3 Answers  


What is a Foreign Key?

0 Answers   Tavant Technologies, Zensar,


is it possible to get -811 error when you use cursors. why?

2 Answers   IBM,


What is rct?

0 Answers  


What is error -818 in db2. where can you find the timestamp of the DBRM and the source code.( precompile puts the timestamp on dbrm and source code correct?)

2 Answers  


what is the steps followed in EXPLAIN Process or EXPLAIN command. (Explain is for identifying the optimized access path but how or what is the steps for doing the EXPLAIN)

4 Answers   IBM,


If I have a view which is a join of two or more tables, can this view be updateable?

0 Answers  


Explain the function of data manager.

0 Answers  


What is the role of data manager in the db2 database?

0 Answers  


EXPLAIN has output with MATCHCOLS = 0. What does it mean?

2 Answers  


what are stage one indexable predicates?

1 Answers  


Categories