typedef
typedef

Reputation: 1930

postgresql select next 10 after something

Is there any way to select the next 10 rows after the row with ID where rows are ordered by something.

I've come up only with simple solution like:

  1. select all
  2. apply a loop to find ID and then return next 10

Thanks!

Upvotes: 3

Views: 2258

Answers (1)

Filipe Silva
Filipe Silva

Reputation: 21657

You can use LIMIT . See the docs:

SELECT * from tableName
WHERE id > yourID
ORDER BY ID ASC
LIMIT NumRows

sqlfiddle demo

If the rows aren't orderd by ID, you can do:

SELECT * FROM tab1
WHERE orderColumn > (
  SELECT orderColumn from tab1
  WHERE id = YourId
)
ORDER BY orderColumn
LIMIT 10

sqlfiddle demo

Upvotes: 3

Related Questions