My query returns a query result which appears to be sorted, though there is no order by statement available. This could be coz of the clustered index.
Is it wise to depend upon index for sorting?
Is it a best practice to rely on index for sorting?
Thanks
Yes, the data appears pre-sorted because of the clustered index. A clustered index means that the rows are physically arranged in the order of the index, which in most (not all) cases is how they will be retrieved.
If there is no clustered, index, rows will be retrieved in the same order that they were inserted.
No, it is not wise to depend on this. If you decide to do a
JOIN,DISTINCT, or make any other change to the query, you can easily break the sorting order. On the other hand, if you use anORDER BYand SQL Server knows that the data is already in that order, it will optimize out the redundant sort and therefore cost you nothing.Therefore, always
ORDER BY, even if it looks like you don’t need it.