DEV Community

Discussion on: Offset and Cursor Pagination explained

Collapse
 
jackmarchant profile image
Jack Marchant

The SQL you have is very close:

Offset
select * from posts LIMIT 20 offset 20;
If you were to paginate every 20 results, then offset would increase/decrease by 20 each subsequent query.

Cursor
select * from posts where id < 40 LIMIT 20;
The cursor is id here, but it could be any column, even a dedicated cursor.

Hope that helps.

Collapse
 
dheerajpande profile image
Dheeraj • Edited

for the cursor query

select * from posts where id < 40 LIMIT 20;

how can we guarantee which 20 to pick?

like lets say that there are 40 records with id 1 to 40,

if I am doing "prev" with id "40" I should get id 20 to 39.

With that be the case here?