Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have rows in sql table i need to pick up 5th row alone how can i?
Posted
Comments
Maciej Los 20-Jun-12 8:34am    
Yes, you can! But i think, this answer ain't satisfy you.
Please, be more specific and provide more detalis: the structure of database and so on...

Use ROW_NUMBER()

I am giving an example
SQL
WITH [EMPLOYEE ORDERED BY ROWID] AS
(SELECT ROW_NUMBER() OVER (ORDER BY EMPID ASC) AS ROWID, * FROM EMPLOYEE)
SELECT * FROM [EMPLOYEE ORDERED BY ROWID] WHERE ROWID =5


The above query will return only 5th row of the table.
 
Share this answer
 
v2
Comments
Maciej Los 20-Jun-12 8:42am    
Wrong answer! My 1, because the MS Sql Server 2005 doesn't have the LIMIT syntax/condition!
Read my comment to the question and answer.
bhagirathimfs 20-Jun-12 8:49am    
sorry for the wrong answer :(
I think limit is there in sql 2005.
Sorry
bhagirathimfs 20-Jun-12 9:07am    
I changed the query.
Maciej Los 20-Jun-12 9:22am    
Now, the query looks correct!
Re-vote, 5!
bhagirathimfs 20-Jun-12 9:26am    
Thanks
First of all, read my comment to the question and comment to the solution no. 1.

Secondly, read it: http://stackoverflow.com/questions/10387386/equivalent-of-limit-in-t-sql[^]
SQL
WITH Members  AS
(
    SELECT  M_NAME, M_POSTS, M_LASTPOSTDATE, M_LASTHEREDATE, M_DATE, M_COUNTRY,
            ROW_NUMBER() OVER (ORDER BY M_POSTS DESC) AS RowNumber
    FROM    dbo.FORUM_MEMBERS
)
SELECT  RowNumber, M_NAME, M_POSTS, M_LASTPOSTDATE, M_LASTHEREDATE, M_DATE, M_COUNTRY
FROM    Members
WHERE   RowNumber BETWEEN 1 AND 20
ORDER BY RowNumber ASC;

and this: http://msdn.microsoft.com/en-us/library/ms175972.aspx[^]

I think, depend on your conditions, you can use something like this to return praticular row:
SQL
SELECT *
FROM Yourtable
WHERE FieldID = @ID


More at: http://msdn.microsoft.com/en-us/library/ms188047.aspx[^]

You can use TOP clause to:
SQL
SELECT TOP(1) *
FROM (SELECT TOP(5) * FROM YourTable ORDER BY FieldID DESC) AS DT

More at: http://msdn.microsoft.com/en-us/library/ms189463.aspx[^]
 
Share this answer
 
Comments
ssd_coolguy 20-Jun-12 9:02am    
correct answer my 5!!
Maciej Los 20-Jun-12 9:05am    
Thank you ;)
VJ Reddy 20-Jun-12 12:56pm    
Very good answer. 5!
Maciej Los 20-Jun-12 15:11pm    
Thank you, VJ ;)
Manas Bhardwaj 21-Jun-12 4:41am    
good +5
SQL
select MAX(model) from Product where model in( select top 5 model from Product order by model );
 
Share this answer
 
v2
If you are using Oracle there are 2 ways in my opinion to do it.

Use nested subquery to get the n-th row:
SQL
select * from (
select A, B, rownum Ind from Table_1
where rownum <= 5
order by A, B
)
where Ind = 5


Or group it by acquiring columns and use "having" clause which also works:
SQL
select A, B, rownum, count(*) from Table_2
group by A, B, rownum
having rownum = 5
order by A, B
 
Share this answer
 
Comments
Maciej Los 20-Jun-12 8:56am    
Not Oracle, tag: SQL Server 2005!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900