select Nth row from table

Posted on 2002-05-10
Medium Priority
Last Modified: 2008-03-06
I have a table with a billion rows
id (primary key, identity), email, firstname, etc...

I want to grab a specific row number, but the id's have gaps (from deleting rows)

my first attempt:
select top 1 * from mytable e where (select count(id) from mytable where id <= e.id) = 412

that would return the 412th row regardless of gaps in id.. the problem with that query is its sooo slow!

can anyone help me with a better solution?

maybe a better idea would be to re-index the table whenever a row is deleted but I could not figure out how to do that either (without making a temporary table) BTW: no temp tables.. the table is toooo BIG to be duplicated
Question by:gfody
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 69

Accepted Solution

Scott Pletcher earned 800 total points
ID: 7002151
If you're going after a relatively low-numbered row, such as 412, this should work OK:

FROM myTable
ORDER BY id) AS topOfMyTable
LVL 69

Expert Comment

by:Scott Pletcher
ID: 7002159
Naturally you will need an index on id (as the first/only column in the index) for this to run efficiently.  You should probably remove it from the other index unless it's needed there to specify order.

Also, since the TOP clause will not accept a variable, with this method you would have to use sp_executesql or EXEC('SELECT ...') to make the row number dynamic.

Author Comment

ID: 7002321
thank you, this is much faster

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question