Oracle default order by behavior

Angshuman :

In select query when we add order by on some column and in the returned resultset that column contains same value for some of the rows what will be the sorting behavior of those rows?

Tim Biegeleisen :

There is no defined sorting behavior, in general, at least the ANSI standard does not require a database vendor to provide one. You might find that a certain order tends to be obeyed, but the best thing to do here would be to just a second sort level to break the tie, i.e. use:

SELECT *
FROM yourTable
ORDER BY
    col1,
    col2;    -- break the tie

So if your data were:

col1 | col2
1    | 1
1    | 2
2    | 4
2    | 1

then the above query would return a sorted result as you expect.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=327191&siteId=1