SQLite classic ...login
SQLite classic tutorial
author:php.cn  update time:2022-04-13 17:05:02

SQLite Order By


SQLite's ORDER BY clause is used to sort 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 multiple columns in the ORDER BY clause. Make sure the sort column you are using is in the column list.

Example

Assuming the COMPANY table has the following records:

# ID name Age Address Salar
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------- ----------- ---------- ----------
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 Jamest On 10000.0
The following is an example, which will sort the results in SALARY ascending order:

##sqlite> SELECT * FROM COMPANY ORDER BY SALARY ASC;

This will produce the following results:

ID                                                                                                                                                                                                         ------ ---------- ----------
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
## This is an instance, It will sort the results by NAME and SALARY in ascending order:

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

This will produce the following results:

ID                                                                                                                                                                                                         ------ ---------- ----------
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                                                                                                          It will sort the results in descending order by NAME:

##sqlite> SELECT * FROM COMPANY ORDER BY NAME DESC;

This will produce the following results:

ID                                                                                                                                                                                                         ------ ---------- ----------
3                                                                                  Teddy                                                          out   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