Portal | IBM Manuals | Downloads | Products | Refer | Info | Programs | JCLs | Forum Rules*| Site Map | Mainframe CD 
IBMMAINFRAMES.com - IBM Mainframe Support Forums Index
 
Register
 
IBMMAINFRAMES.com - IBM Mainframe Support Forums Index FAQ Search Memberlist Usergroups Profile Log in to check your private messages Log in
 
How to find second row from table?

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> Interview Questions
Author Message
Sambhaji
Warnings : 1

Active User


Joined: 16 Feb 2007
Posts: 268
Location: Pune, India

PostPosted: Tue Aug 26, 2008 12:17 pm    Post subject: How to find second row from table?
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
References
Prasanthhere

Active User


Joined: 03 Aug 2005
Posts: 279

PostPosted: Tue Aug 26, 2008 1:09 pm    Post subject: Reply to: How to find second row from table?
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
Sambhaji
Warnings : 1

Active User


Joined: 16 Feb 2007
Posts: 268
Location: Pune, India

PostPosted: Tue Aug 26, 2008 1:45 pm    Post subject:
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

Global Moderator


Joined: 14 Mar 2007
Posts: 3083
Location: italy

PostPosted: Tue Aug 26, 2008 2:10 pm    Post subject: Reply to: How to find second row from table?
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

Senior Member


Joined: 20 Oct 2006
Posts: 1618
Location: germany

PostPosted: Tue Aug 26, 2008 2:36 pm    Post subject:
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
Sambhaji
Warnings : 1

Active User


Joined: 16 Feb 2007
Posts: 268
Location: Pune, India

PostPosted: Wed Aug 27, 2008 11:20 am    Post subject:
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
Sambhaji
Warnings : 1

Active User


Joined: 16 Feb 2007
Posts: 268
Location: Pune, India

PostPosted: Wed Aug 27, 2008 11:23 am    Post subject:
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
Sambhaji
Warnings : 1

Active User


Joined: 16 Feb 2007
Posts: 268
Location: Pune, India

PostPosted: Wed Aug 27, 2008 2:18 pm    Post subject:
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: 125
Location: Bangalore

PostPosted: Sun Aug 31, 2008 8:44 pm    Post subject:
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
http://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
Display posts from previous:   
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> Interview Questions All times are GMT + 6 Hours
Page 1 of 1