Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 955
kode master Avatar asked Apr 30 '26 11:04

kode master


1 Answers

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.

like image 197
Sergey Kalinichenko Avatar answered May 03 '26 08:05

Sergey Kalinichenko