PostgreSQL

Order By

The ORDER BY statement sorts the retrieved rows in ascending (default) or descending order. In ascending order NULL values will come first and in descending order NULL values will come last.

Syntax

Default (Ascending)

SELECT <query-parameters> FROM <table-name> ORDER BY <column-name>;

Explicit Ascending

SELECT <query-parameters> FROM <table-name> ORDER BY <column-name> ASC;

Explicit Descending

SELECT <query-parameters> FROM <table-name> ORDER BY <column-name> DESC;

Example #1

SELECT * FROM Companies ORDER BY founded;
Result from example #1
Name Type Founded HQ
Disney Entertainment 1923 United States
Walmart Retail 1962 United States
Blackberry Technology 1984 Canada
PayPal Finance 1998 United States

Example #2

SELECT * FROM Companies ORDER BY name DESC;
Result from example #2
Name Type Founded HQ
Walmart Retail 1962 United States
PayPal Finance 1998 United States
Disney Entertainment 1923 United States
Blackberry Technology 1984 Canada