?
Solved

TSQL: SELECT TOP 15 to 30 records?

Posted on 2009-02-17
7
Medium Priority
?
878 Views
Last Modified: 2012-05-06
Hi All,

I know it is possible to select the top 15 records via SELECT TOP 15, but how do i select the next 15.

i.e.
SELECT TOP 15 to 30 ?
0
Comment
Question by:detox1978
  • 5
  • 2
7 Comments
 
LVL 42

Expert Comment

by:dqmq
ID: 23665505
For 2005 and later, this is probably the best approach:
select * from
(select *, row_number() over(order by col1) seq from Yourtable)
where seq between 16 and 30

0
 
LVL 2

Author Comment

by:detox1978
ID: 23665543
I Get an error;

Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'where'.
select * from
(select *, row_number() over(order by [ID]) seq from [MyDatabase].[dbo].[MyTable])
where seq between 16 and 30

Open in new window

0
 
LVL 2

Author Comment

by:detox1978
ID: 23665561
I'm using SQL express 2005
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 42

Accepted Solution

by:
dqmq earned 2000 total points
ID: 23665572
select * from
(select *, row_number() over(order by [ID]) seq from [MyDatabase].[dbo].[MyTable]) x
where seq between 16 and 30
0
 
LVL 2

Author Closing Comment

by:detox1978
ID: 31548067
thanks
0
 
LVL 2

Author Comment

by:detox1978
ID: 23666092
Hi,

How do i filter the results above by another select query, so that only ID that are returned from another select statment below below are returned?

many thanks
SELECT [ID] FROM [MyDatabase].[dbo].[MySecondTable]) Where [best]='5'

Open in new window

0
 
LVL 2

Author Comment

by:detox1978
ID: 23666279
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

840 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