How to retrieve specific rows from the SQL Server table?

advertisements

I was wondering is there a way to retrieve, for example, 2nd and 5th row from SQL table that contains 100 rows?

I saw some solutions with WHERE clause but they all assume that the column on which WHERE clause is applied is linear, starting at 1.

Is there other way to query a SQL Server table for a specific rows in case table doesn't have a column whose values start at 1?

P.S. - I know for a solution with temporary tables, where you copy your select statement output and add a linear column to the table. I am using T-SQL


Try this,

SELECT * FROM (
  SELECT
    ROW_NUMBER() OVER (ORDER BY ColumnName ASC) AS rownumber
    FROM TableName
)  as temptablename
WHERE rownumber IN (2,5)