99

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
2240
  • 1,547
  • 2
  • 12
  • 30
pierrotlefou
  • 39,805
  • 37
  • 135
  • 175

3 Answers3

140
ORDER BY col_A, col_B

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

James McNellis
  • 348,265
  • 75
  • 913
  • 977
25

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.

meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
11
SELECT * FROM tbl WHERE predictor ORDER by col_A, col_B
Jason Leveille
  • 1,190
  • 5
  • 10