kode master
kode master

Reputation: 100

SQL: Sorting the same column asc then desc

I tried using order by clause to sort and re-sort the data on the same column. My query was:

SELECT * FROM Customers ORDER BY Country ASC, Country DESC;

The result is surprising. It sorts the Country Column in Ascending Order only.

According to my knowledge, the Country Column should have been sorted in an Ascending order first, then in a Descending order.

Why did SQL skip the next part of the query?

Upvotes: 3

Views: 3444

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

When you specify multiple items in ORDER BY clause, the ordering is determined as follows:

  • Results are sorted using the first order specification (column name + direction)
  • Any ties that remain are resolved using the second order specification,
  • Any ties that remain are resolved using the third order specification, and so on.

Resolving ties using the same column, regardless of the direction, wouldn't change the ordering, because the values in the column are the same within the tied group.

Upvotes: 7

Related Questions