To sort the table returned by the query, simply include the ORDER BY <fields> clause. Note that in order for it to work, the fields in <fields> must also be included in the select.

The desc keyword allows you to sort the data in reverse order, that is, descending. SQL

select Name, Surname, Age from Person
order by Age desc;

Pandas In pandas, we can use the sort_values() function.

result = person.sort_values(by="Age", ascending=False)[["Name", "Surname", "Age"]]

When we include multiple fields in the order by clause, we am informing the system to proceed with sorting by the next field in case the previous field is equal for both.

SQL

select * from Person
order by Age desc, Name

In this case, the system will sort the people by age (in descending order), and those with the same age will be sorted by name (in ascending order by default).

Pandas We can do the same in pandas, defining a list of columns name and a list of boolean, which maps for each column if the order should be ascending or descending.

result = person.sort_values(by=["Age", "Name"], ascending=[False, True])[["Name", "Surname", "Age"]]