In SQLite the ORDER BY clause can be used to sort the result based on more than one columns.
Using ORDER BY , data can be sorted eighther in ascending order or in descending order.
ASC : Sorts tha result set in ascending order.
DESC : Sorts tha result set in descending order..
SELECT column_1, column_2, column_n FROM table_name WHERE [condition_1] [ORDER BY column_1, column_2, .. column_n] [ASC];
One or more columns can be used to sort the data.
So let's consider we have created SCHOOL database. And we have created a STUDENTS table in this database.
The STUDENTS table have five columns (ID, NAME, SURNAME, AGE, ADDRESS).
The STUDENTS table also have some data inside it as shown below:
ID NAME SURNAME AGE ADDRESS ---------- ---------- ---------- ---------- ---------- 1 Mark Osaka 20 Munich 2 Tom white 21 Cologne 3 Patric Rossman 19 Essen 4 Mark Khan 22 Bonn 5 Julia Tesar 18 Berlin 6 Tim Netten 20 Frankfurt 7 Mark Mevric 17 Wuppertal
The following SQLite statement selects the columns from the STUDENTS table ORDER BY AGE in ascending order:
sqlite> SELECT * FROM STUDENTS ORDER BY AGE ASC; ID NAME SURNAME AGE ADDRESS ---------- ---------- ---------- ---------- ---------- 7 Mark Mevric 17 Wuppertal 5 Julia Tesar 18 Berlin 3 Patric Rossman 19 Essen 1 Mark Osaka 20 Munich 6 Tim Netten 20 Frankfurt 2 Tom white 21 Cologne 4 Mark Khan 22 Bonn
SELECT column_1, column_2, column_n FROM table_name WHERE [condition_1] [ORDER BY column_1, column_2, .. column_n] [DESC];
The following SQLite statement selects the columns from the STUDENTS table ORDER BY AGE in descending order:
sqlite> SELECT * FROM STUDENTS ORDER BY AGE DESC; ID NAME SURNAME AGE ADDRESS ---------- ---------- ---------- ---------- ---------- 4 Mark Khan 22 Bonn 2 Tom white 21 Cologne 6 Tim Netten 20 Frankfurt 1 Mark Osaka 20 Munich 3 Patric Rossman 19 Essen 5 Julia Tesar 18 Berlin 7 Mark Mevric 17 Wuppertal