Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SQLite Order By

SQLite Order By

SQLite's ORDER BY ORDER BY clause is used to arrange data in ascending or descending order based on one or more columns.

Syntax

The basic syntax of the ORDER BY clause is as follows:



SELECT column-list 

FROM table_name 

[WHERE condition] 

[ORDER BY column1, column2, .. columnN] [ASC | DESC];

You can use multiple columns in the ORDER BY clause; make sure the sorting columns you use are in the column list:

SELECT

   select_list

FROM

   table

ORDER BY

    column_1 ASC,

    column_2 DESC;

column_1 and column_2, if no sorting rule is specified after them, default to ASC ascending; the above statement reads column_1 in ascending order and column_2 in descending order, equivalent to the following statement:

SELECT

   select_list

FROM

   table

ORDER BY

    column_1,

    column_2 DESC;

Example

Assume the COMPANY table has the following records:



ID          NAME        AGE         ADDRESS     SALARY

----------  ----------  ----------  ----------  ----------

1           Paul        32          California  20000.0

2           Allen       25          Texas       15000.0

3           Teddy       23          Norway      20000.0

4           Mark        25          Rich-Mond   65000.0

5           David       27          Texas       85000.0

6           Kim         22          South-Hall  45000.0

7           James       24          Houston     10000.0

The following is an example that sorts the results by SALARY in ascending order:



sqlite> SELECT * FROM COMPANY ORDER BY SALARY ASC;

This will produce the following result:



ID          NAME        AGE         ADDRESS     SALARY

----------  ----------  ----------  ----------  ----------

7           James       24          Houston     10000.0

2           Allen       25          Texas       15000.0

1           Paul        32          California  20000.0

3           Teddy       23          Norway      20000.0

6           Kim         22          South-Hall  45000.0

4           Mark        25          Rich-Mond   65000.0

5           David       27          Texas       85000.0

The following is an example that sorts the results by NAME and SALARY in ascending order:



sqlite> SELECT * FROM COMPANY ORDER BY NAME, SALARY ASC;

This will produce the following result:



ID          NAME        AGE         ADDRESS     SALARY

----------  ----------  ----------  ----------  ----------

2           Allen       25          Texas       15000.0

5           David       27          Texas       85000.0

7           James       24          Houston     10000.0

6           Kim         22          South-Hall  45000.0

4           Mark        25          Rich-Mond   65000.0

1           Paul        32          California  20000.0

3           Teddy       23          Norway      20000.0

The following is an example that sorts the results by NAME in descending order:



sqlite> SELECT * FROM COMPANY ORDER BY NAME DESC;

This will produce the following result:



ID          NAME        AGE         ADDRESS     SALARY

----------  ----------  ----------  ----------  ----------

3           Teddy       23          Norway      20000.0

1           Paul        32          California  20000.0

4           Mark        25          Rich-Mond   65000.0

6           Kim         22          South-Hall  45000.0

7           James       24          Houston     10000.0

5           David       27          Texas       85000.0

2           Allen       25          Texas       15000.0