educative.io

Order By Column Priority

I just have a little confusion and I hope you will clear it up.

Syntax: Order By col1, col2, colN. The col1 has high priority while sorting the records and col2 will have less than col1 and so on. Now my question is when there are multiple columns available for sorting then it will be ordered according to col1 and will sort according to col2 if and only if any two or more rows have the same values in col1.

Thanks


Course: https://www.educative.io/collection/5352985413550080/6335716377231360
Lesson: https://www.educative.io/collection/page/5352985413550080/6335716377231360/6683698016550912

Hi @ahmed_anwar, Thanks for reaching out to us.
In this case, if a tie (same values in two rows) occurs based on the first sort key, it is broken using the second sort key.
For example, some actors in our table have the same net worth. We can specify net worth as the first sort key and the first name as the second sort key.

Note the highlighted lines above show Brad Pitt first and Jennifer Aniston second even though both have the same net worth as the tie is broken by the second sort key which is the first name.

Hope it will clear your confusion :slight_smile:

If we sort by multiple columns, then sorting is done through the first column only and will only be done through the second column if a tie happens as we have seen above. It means only first column will be used for sorting in multiple-column ordering.


Course: https://www.educative.io/collection/5352985413550080/6335716377231360
Lesson: https://www.educative.io/collection/page/5352985413550080/6335716377231360/6068168201076736

Hi @ahmed_anwar
Yes, excatly.

1 Like