neolaser neolaser - 7 months ago 15
SQL Question

SQL Server select rows between two values

Hopefully the title explains it enough, but I want to be able to select rows in an SQL Server table between two values

example

SELECT * FROM table WHERE rows between 20 and 60


I have tried the ROW_NUMBER() and then use a WHERE clause....

Thanks,

Jason

Answer
SELECT TOP 40 * FROM (SELECT TOP 60 * FROM table ORDER BY xx ASC) t ORDER BY xx DESC

Since the output of a select statement can return records in any order (without an order by clause) you need to decide which order to apply to the records... use the primary key if you don't know or care (substitute for xx)

Comments