0
SELECT *
FROM users
ORDER BY highscore DESC
LIMIT 5 OFFSET 5

and

SELECT *
FROM users
ORDER BY highscore DESC
LIMIT 5 OFFSET 10

returning the same result. And they are different than the records when I omit the LIMIT clause! I searched the community. There are similar questions, but they are of no help.

EDIT: Here are the table data-

Here is the table data ordered by <code>highscore</code>

7
  • Is there any restriction to prevent users from having the same highscore? Commented Jul 3, 2017 at 10:17
  • No restrictions. I should also mention that many of them have the same high score. Commented Jul 3, 2017 at 10:18
  • 2
    show us table data Commented Jul 3, 2017 at 10:20
  • Please check the result of SELECT * FROM users ORDER BY highscore DESC LIMIT 15. It's possible that values are same. Commented Jul 3, 2017 at 10:22
  • Keyur Panchal. Yes, the high score values are the same for them. They are all 0. Commented Jul 3, 2017 at 10:24

1 Answer 1

2

Presumably, the issue is that you have ties for highscore. When you have ties, then MySQL orders the rows with the same value in an arbitrary and indeterminate way. Even two runs of the same query can result in different orderings.

Why? The reason is simple. There is no "natural" order for sorting keys with the same value. SQL tables represent unordered sets.

To make the sorting stable, include a unique id as the last key in the ORDER BY:

SELECT u.*
FROM users u
ORDER BY u.highscore DESC, u.userId
LIMIT 5 OFFSET 5;

Then, when you fetch the next 5 rows, they will be different.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.