1

Does the sequence of records in SELECT QUERY in result always the same? I mean that if the first result of operation returns the following sequence: first record second record third record All other select * from t queries always return records in the same sequence.

1
  • No, that's not guaranteed unless you use ORDER BY to explicitly sort the result. Commented Jan 14, 2019 at 12:01

1 Answer 1

6

A SQL query -- like a SQL table -- represents an unordered set. There is no ordering, unless an ORDER BY is present for the outermost SELECT.

As an unordered set, the same query can return results in a different order each time it is run.

So, if you want results in a particular order, use ORDER BY.

I should add that if multiple rows have the same key, then these rows can appear in any order, even with an ORDER BY. In general, you should ensure that the keys in the ORDER BY uniquely define each row (say by including the primary key as the final key).

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.