JANARTHANAN M
JANARTHANAN M

Reputation: 1

Selecting 3+ rows in SQL

how to select 1st, 4th, 7th, 10th... row from a table in SQL if i HAVING RECORDS LIKE BELOW

id  Name
1   a
2   b
3   c
4   b
5   s
6   h
7   k
8   g

i need to select 1 st, 4th, 7th, 10th rows

pls help me thanks Janarthanan M

Upvotes: 0

Views: 66

Answers (1)

Thomas Mueller
Thomas Mueller

Reputation: 50107

I don't have access to SQL Server right now, but I believe this should work. It works for PostgreSQL:

SELECT * FROM
(SELECT 
ROW_NUMBER () OVER (ORDER BY id) AS RowNumber, 
id, name FROM test) X
WHERE (RowNumber % 3) = 1

Upvotes: 6

Related Questions