How to order by column A and then by column B?

SqlSelectSql Order-By

Sql Problem Overview


How to write SQL so that the result can be ordered first by column A then by column B. Something like below:

SELECT * FROM tbl WHERE predictor ORDER by col_A and ORDER by col_B

Sql Solutions


Solution 1 - Sql

ORDER BY col_A, col_B

The SQLite website has syntax diagrams explaining the SQL grammar supported by SQLite.

Solution 2 - Sql

Just feed a comma separated list of columns to ORDER BY:

SELECT * from table WHERE table.foo=bar ORDER BY colA, colB

> The ORDER BY clause causes the output > rows to be sorted. The argument to > ORDER BY is a list of expressions that > are used as the key for the sort. The > expressions do not have to be part of > the result for a simple SELECT, but in > a compound SELECT each sort expression > must exactly match one of the result > columns. Each sort expression may be > optionally followed by a COLLATE > keyword and the name of a collating > function used for ordering text and/or > keywords ASC or DESC to specify the > sort order.

Solution 3 - Sql

SELECT * FROM tbl WHERE predictor ORDER by col_A, col_B

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
QuestionpierrotlefouView Question on Stackoverflow
Solution 1 - SqlJames McNellisView Answer on Stackoverflow
Solution 2 - Sqlmeder omuralievView Answer on Stackoverflow
Solution 3 - SqlJason LeveilleView Answer on Stackoverflow