Multiple level ordering

SqlSqliteSorting

Sql Problem Overview


I have a table with some records with fields like name, rating etc.

I first want to sort based on rating limiting results to 20 and then on this resultset want to further apply sort based on name.

I know to sort we need to use the query like

Select * from table order by rating Desc limit 20

but on this resultset how to apply another level of ordering? How can I combine these two sorts in one sqlite statement?

Sql Solutions


Solution 1 - Sql

You could use e.g. ORDER BY rating DESC, name ASC to sort by rating and then, if the ratings are equal, by name.

Solution 2 - Sql

This query should do the trick:

SELECT * FROM (SELECT * FROM table ORDER BY rating DESC LIMIT 20) ORDER BY name

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSapanView Question on Stackoverflow
Solution 1 - SqlThiefMasterView Answer on Stackoverflow
Solution 2 - SqlJuanBocaView Answer on Stackoverflow