I have a query that in fact uses ORDER BY but not on enough columns to make each row unique on these columns. Someone else has a similar query, structured a little differently but essentially returning the same data. The two queries are returning data in slightly different order.
I intend to extend the order by clause to resolve the issue. However the lists are public and the order is "sensitive" ( a waiting list )
If instead of extending the order by clause, I made my query identical to the other one:
1) Would I always get the same results as the other query ?
2) Would I get the same results day after day ?
The data is in fact quite dynamic but it is important that both queries return the same order.
The answer to both questions is "no". The order might be the same when you test it, and it might even continue to be the same for a very long time, but this is not guaranteed - if it remains the same, you are just lucky,
The only guarantee given by the ORDER BY is the that the specified ordering is applied; rows with identical values in the ORDER BY columns can be in any order. If you want controle over that order, add tiee-breaker columns to the ORDER BY until you get a list of columns whhere no rows will ever have duplicate values.
BTW, you seem to have misplaced this question. I hope a moderator will move it to a more appropriate location.
Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis