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
Solved

select the nth row of a table

Posted on 2013-01-09
12
478 Views
Last Modified: 2013-01-13
how can i easily to select the nth row of a table in SQL query.

For example, if i want to select the 3rd of a table, how can i do that?
(notes: if the table does not have row number)
0
Comment
Question by:hongclub
12 Comments
 
LVL 2

Expert Comment

by:thombeck
ID: 38760423
you can use this code:

SELECT * FROM (
  SELECT
    ROW_NUMBER() OVER (ORDER BY key ASC) AS rownumber,
    columns
  FROM tablename
) AS foo
WHERE rownumber <= n
0
 
LVL 12

Expert Comment

by:Jared_S
ID: 38760429
use row_number()
post some details of your data and we can build the query.
0
 
LVL 31

Expert Comment

by:James Murrell
ID: 38760434
SQL 2005 and above has this feature built-in. Use the ROW_NUMBER() function. It is excellent for web-pages with a << Prev and Next >> style browsing:

Syntax:

SELECT * FROM
(SELECT ROW_NUMBER() OVER (ORDER BY MyColumnToOrderBy) AS RowNum, * FROM Table_1) sub
WHERE RowNum = 23

Taken fromhttp://stackoverflow.com/questions/16568/how-to-select-the-nth-row-in-a-sql-database-table

I use it all the time: had link bookmarked
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38760457
@cs97jjm3's method is excellent, I use it myself for pagination.  But... SQL tables do not have built-in 'row numbers' like spreadsheets do.  ROW_NUMBER() Depends on the ORDER BY statement for it's order.  If you insert a value that changes the ORDER BY, you will change the Row Number for a particular item.
0
 

Author Comment

by:hongclub
ID: 38760463
i used that too, but cuz in my sql has select distinct, so when i use the ROW_NUMBER, it return more rows than i though

select Distinct col1, ROW_NUMBER() Over(Order By col1), col2from #tempResult  order by col2 asc
0
 
LVL 41

Expert Comment

by:ralmada
ID: 38760505
maybe you should try with dense_rank

dense_rank() Over(Order By col1)

More info on dense_rank here:

http://msdn.microsoft.com/en-us/library/ms173825.aspx
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 38761410
i used that too, but cuz in my sql has select distinct, so when i use the ROW_NUMBER, it return more rows than i though
Try it this way:
SELECT  col1,
        col2,
        ROW_NUMBER() OVER (ORDER BY col1) Row
FROM    (SELECT DISTINCT
                col1,
                col2
         FROM   #tempResult
        ) d
WHERE Row = 3
ORDER BY col2 ASC

Open in new window

0
 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 38761785
In 2005/2008

SELECT  col1,
        col2,
        ROW_NUMBER() OVER (ORDER BY col1) Row
FROM    (SELECT DISTINCT
                col1,
                col2
         FROM   #tempResult
        ) d
WHERE Row = 3
ORDER BY col2 ASC

As Above solutions provided by other experts

In SQL Server 2012

select * from sys.objects where modify_date > format(DateAdd(DAy, -1, getdate()),'yyyyMMdd')
order by name
Offset 10 rows Fetch Next 1 rows only
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38765861
To clarify:  The comment awarded points is an exact copy and paste of the solution I posted previously here http:#a38761410
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38769175
Please leave your recommendations here as to how you wish to see this question closed.
3) http:#a38761410
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create SQL Server Job to perform backups of all the databases in a server instance 17 40
SSRS Deployment problem 5 64
Database Integrity 1 49
SQL USE DATABASE VARIABLE 5 27
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

856 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