Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

SQLite Order By


May 16, 2021 SQLite


Table of contents


SQLite Order By

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

Grammar

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 more than one column in the ORDER BY clause. Make sure that the row sequence you are using is in the list of columns.

Suppose 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

Here's an example that sorts the results in SALARY ascending order:

sqlite> SELECT * FROM COMPANY ORDER BY SALARY ASC;

This results in the following:

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

Here's an example that sorts the results in NAME and SALARY ascending order:

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

This results in the following:

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

Here's an example that sorts the results in descending order of NAME:

sqlite> SELECT * FROM COMPANY ORDER BY NAME DESC;

This results in the following:

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