DEV Community

Discussion on: 1 SQL Query You Should Stop Using

Collapse
 
abdisalan_js profile image
Abdisalan

I would do the same as the created_at example. Order your query by the name whether asc or desc and then use the where clause to filter the names you've already seen.

If page one is:
AAA
AAB
AAC

your query could be

SELECT * from users
WHERE name < 'AAC'
ORDER BY name DESC
LIMIT 3

Its a little bit harder if the user can sort things by a lot of different factors. You can make the decision to just use OFFSET anyway depending on how much data you have