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
After the ORDER BY keyword, add the name of the column by which you'd like to sort records first (in our example, salary). Then, after a comma, add the second column (in our example, last_name ). You can modify the sorting order (ascending or descending) separately for each column.
If you specify multiple columns, the result set is sorted by the first column and then that sorted result set is sorted by the second column, and so on. The columns that appear in the ORDER BY clause must correspond to either column in the select list or columns defined in the table specified in the FROM clause.
By using ORDER BY clause, we can sort the result in ascending or descending order. This clause can be used with multiple columns as well.
So you want to order by one column first and then another? You can specify more than one column in the ORDER BY clause of a query - separate them by commas, and the first one will be the 'major' sort, then subsequent columns in the list will be sorted within that.
ORDER BY col_A, col_B
The SQLite website has syntax diagrams explaining the SQL grammar supported by SQLite.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With