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
| id | good_type | name | description | price |
|---|
| 1 | A | Car_1 | Car 1 description | 100 |
| 2 | A | Car_2 | Car 2 description | 200 |
| 3 | A | Car_3 | Car 3 description | 100 |
| 4 | B | Boat_4 | Boat 4 description | 500 |
| 5 | B | Boat_5 | Boat 5 description | 300 |
| 6 | C | Train_1 | Train 123 description | 800 |
select * from goods order by price asc;
Result
| id | good_type | name | description | price |
|---|
| 1 | A | Car_1 | Car 1 description | 100 |
| 3 | A | Car_3 | Car 3 description | 100 |
| 2 | A | Car_2 | Car 2 description | 200 |
| 5 | B | Boat_5 | Boat 5 description | 300 |
| 4 | B | Boat_4 | Boat 4 description | 500 |
| 6 | C | Train_1 | Train 123 description | 800 |
select * from goods where price=100 order by id desc;
Result
| id | good_type | name | description | price |
|---|
| 3 | A | Car_3 | Car 3 description | 100 |
| 1 | A | Car_1 | Car 1 description | 100 |