Is there a possibility where the same query executed at time T2 yields a result that has the same R number of records but with the order of the records changed?
Absolutely! This is not just a possibility, it is how the SQL language is defined. SQL tables and results sets represent unordered (multi)sets. The only exception is for a result set generated by a query that has an explicit ORDER BY
in the outermost query.
When does this occur? Actually, it occurs regularly on any query running on largish tables in a parallel environment. In a parallel environment, the work is distributed to nodes and when the nodes complete, the results are gathered and returned to the user. Further, the algorithms used to run the query might incorporate randomness for distributing records to the nodes.
In practice, you never see a difference on queries on small tables. These normally fit on a single data page and SQL engines are only using the simplest processing.
If you care about the ordering of the result set, then include ORDER BY
. If you just want to see if the query returns the same results as the last time it was run, then use ORDER BY
, so you can easily compare row-by-row.