DEV Community

Discussion on: 1 SQL Query You Should Stop Using

Collapse
thechrisjasper profile image
Chris Jasper

And how would you propose that you page data that is ordered by the user? For example, a name column that a user can toggle asc/desc?

Collapse
abdisalan_js profile image
Abdisalan Author

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