IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

How to find second row from table?


IBM Mainframe Forums -> Mainframe Interview Questions
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Escapa

Senior Member


Joined: 16 Feb 2007
Posts: 1399
Location: IL, USA

PostPosted: Tue Aug 26, 2008 12:17 pm
Reply with quote

Can anybody give me SQL query to find second row in the table?
Back to top
View user's profile Send private message
Prasanthhere

Active User


Joined: 03 Aug 2005
Posts: 306

PostPosted: Tue Aug 26, 2008 1:09 pm
Reply with quote

I believe there is no first row, no second row etc in a Db2. The ordering is immaterial.
Back to top
View user's profile Send private message
Escapa

Senior Member


Joined: 16 Feb 2007
Posts: 1399
Location: IL, USA

PostPosted: Tue Aug 26, 2008 1:45 pm
Reply with quote

I didn't get what you are trying to explain but i will elaborate more..
What i want is
if you do select * from table it will return all the rows. I want to fetch second row from that set.
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Tue Aug 26, 2008 2:10 pm
Reply with quote

Your interviewer should review his understanding of db2/sql,

in a relational data base there i no inherent(positional) ordering,

asking for the second row might yield different results at different times
( You can experiment that after a reload )
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Tue Aug 26, 2008 2:36 pm
Reply with quote

at the same time, maybe the interviewer asks this question to see if the interviewee knows that this is a trick question.

otherwise, as you said Enrico, the interviewee should answer as you indicated.
Back to top
View user's profile Send private message
Escapa

Senior Member


Joined: 16 Feb 2007
Posts: 1399
Location: IL, USA

PostPosted: Wed Aug 27, 2008 11:20 am
Reply with quote

Thanks. That would really help my rdbms knowledge..

But I would like to reframe Question as:

I have table like this

Name(*) Coupon_no(integer)
Abby 2122
Brad 1027
Cally 9023
Doc 2122
Mac 9278

I want to retrieve second row when it is ordered by Name
Back to top
View user's profile Send private message
Escapa

Senior Member


Joined: 16 Feb 2007
Posts: 1399
Location: IL, USA

PostPosted: Wed Aug 27, 2008 11:23 am
Reply with quote

Name is char(4).

In above case query should retrieve
row with Brad as primary key.
Back to top
View user's profile Send private message
Escapa

Senior Member


Joined: 16 Feb 2007
Posts: 1399
Location: IL, USA

PostPosted: Wed Aug 27, 2008 2:18 pm
Reply with quote

Can you please give me db2 query to get second row when it is ordered by name?
Back to top
View user's profile Send private message
Ajay Baghel

Active User


Joined: 25 Apr 2007
Posts: 206
Location: Bangalore

PostPosted: Sun Aug 31, 2008 8:44 pm
Reply with quote

You can use Scrollable cursor with FETCH ABSOLUTE in your application program for such purposes.

First declare the cursor
EXEC SQL DECLARE Cursrname INSENSITIVE SCROLL CURSOR FOR
Your select query
END-EXEC.

Open the cursor

TO fetch the 2nd row of the result table, use a FETCH statement like this:
EXEC SQL FETCH ABSOLUTE +2 Cursrname INTO :row-var END-EXEC

PS:
TO fetch the 2nd row from the end of the result table, use a FETCH statement like this:
EXEC SQL FETCH ABSOLUTE -2 Cursrname INTO :row-var END-EXEC

Please refer
publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.apsg/db2z_retrieverowsscrollablecursor.htm


Thanks,
Ajay
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> Mainframe Interview Questions

 


Similar Topics
Topic Forum Replies
No new posts Load new table with Old unload - DB2 DB2 6
No new posts Pulling a fixed number of records fro... DB2 2
No new posts To find whether record count are true... DFSORT/ICETOOL 6
No new posts Multiple table unload using INZUTILB DB2 2
No new posts Check data with Exception Table DB2 0
Search our Forums:

Back to Top