Click here to Skip to main content
15,917,642 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i want to fetch only 5th record from database with or without using top.. how to write a query
Posted
Comments
PIEBALDconsult 23-Nov-13 23:13pm    
What is the 5th record? How would you know it's the 5th record? Does it matter? Can you return just any record and say it's the 5th?
Sergey Alexandrovich Kryukov 24-Nov-13 12:41pm    
Right. That's the whole point: tables don't have any certain record numbers.
—SA

Sounds like an interview question. Have you ever Googled it?

Have a look at several solutions on Google for this HERE.
 
Share this answer
 
try this:

SQL
WITH table2 AS (SELECT (ROW_NUMBER() OVER (ORDER BY table1.someField ASC)) as rownum,* FROM table1)
SELECT * FROM table2 WHERE rownum=5
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 24-Nov-13 12:44pm    
Now do you know ROW_NUMBER even exists? It's better not to assume any rationale behind invalid questions. Most likely, the question is based on one big OP's misconception — please see the comments to the question. Your "answer", even though it has a rational grain, can only further confuse OP.
—SA

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