PostgreSQL Order By

PostgreSQL Order By

The PostgreSQL ORDER BY is used to sort the records of a select.

Order By syntax

SELECT * FROM table_name ORDER BY column_name ASC | DESC;

Order By example

Goods table

idgood_typenamedescriptionprice
1ACar_1Car 1 description100
2ACar_2Car 2 description200
3ACar_3Car 3 description100
4BBoat_4Boat 4 description500
5BBoat_5Boat 5 description300
6CTrain_1Train 123 description800
select * from goods order by price asc;

Result

idgood_typenamedescriptionprice
1ACar_1Car 1 description100
3ACar_3Car 3 description100
2ACar_2Car 2 description200
5BBoat_5Boat 5 description300
4BBoat_4Boat 4 description500
6CTrain_1Train 123 description800
select * from goods where price=100 order by id desc;

Result

idgood_typenamedescriptionprice
3ACar_3Car 3 description100
1ACar_1Car 1 description100