Easy Tutorial
❮ Postgresql Create Table Postgresql Datetime ❯

PostgreSQL ORDER BY Clause

In PostgreSQL, the ORDER BY clause is used to sort data in ascending (ASC) or descending (DESC) order based on one or multiple 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 one or multiple columns in the ORDER BY clause, but the columns to be sorted must exist.

ASC stands for ascending order, and DESC stands for descending order.

Example

Create a COMPANY table (download COMPANY SQL file), with the following data:

tutorialprodb# select * from COMPANY;
 id | name  | age | address   | salary
----+-------+-----+-----------+--------
  1 | Paul  |  32 | California|  20000
  2 | Allen |  25 | Texas     |  15000
  3 | Teddy |  23 | Norway    |  20000
  4 | Mark  |  25 | Rich-Mond |  65000
  5 | David |  27 | Texas     |  85000
  6 | Kim   |  22 | South-Hall|  45000
  7 | James |  24 | Houston   |  10000
(7 rows)

The following example will sort the results in ascending order based on the AGE field:

tutorialprodb=# SELECT * FROM COMPANY ORDER BY AGE ASC;

This will produce the following result:

id | name  | age |                      address                       | salary 
----+-------+-----+----------------------------------------------------+--------
  6 | Kim   |  22 | South-Hall                                         |  45000
  3 | Teddy |  23 | Norway                                             |  20000
  7 | James |  24 | Houston                                            |  10000
  4 | Mark  |  25 | Rich-Mond                                          |  65000
  2 | Allen |  25 | Texas                                              |  15000
  5 | David |  27 | Texas                                              |  85000
  1 | Paul  |  32 | California                                         |  20000
(7 rows)

The following example will sort the results in ascending order based on the NAME and SALARY fields:

tutorialprodb=# SELECT * FROM COMPANY ORDER BY NAME, SALARY ASC;

This will produce the following result:

id | name  | age |                      address                       | salary 
----+-------+-----+----------------------------------------------------+--------
  2 | Allen |  25 | Texas                                              |  15000
5 | David | 27 | Texas | 85000
7 | James | 24 | Houston | 10000
6 | Kim | 22 | South-Hall | 45000
4 | Mark | 25 | Rich-Mond | 65000
1 | Paul | 32 | California | 20000
3 | Teddy | 23 | Norway | 20000
(7 rows)

The following example will sort the result in descending order by the NAME field:

tutorialprodb=# SELECT * FROM COMPANY ORDER BY NAME DESC;

The result is as follows:

id | name | age | address | salary 
----+-------+-----+----------------------------------------------------+--------
3 | Teddy | 23 | Norway | 20000
1 | Paul | 32 | California | 20000
4 | Mark | 25 | Rich-Mond | 65000
6 | Kim | 22 | South-Hall | 45000
7 | James | 24 | Houston | 10000
5 | David | 27 | Texas | 85000
2 | Allen | 25 | Texas | 15000
(7 rows)
❮ Postgresql Create Table Postgresql Datetime ❯