You can sort the results of a query by using ORDER BY.
For example you might want to output the results in order of increasing price, or date, or alphabetically.
ORDER BY does not change the results themselves, it only changes their order.
It looks like this:
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;
ORDER BY is used after you have a completed query and you only want to change the order of the results.
You specify the columns you want to sort on. If I wanted to sort by ascending (increasing) price,
ORDER BY price
If products have the same price, you might also want to control the order. So you can choose a second column where the fields might not be the same, such as name.
ORDER BY price asc, name asc
The columns in ORDER BY must be included in the results of your query. So it's good to create the query before writing ORDER BY, so you can check that the columns you want to sort on are in the results.
If you don't specify ASC or DESC, PostgreSQL will sort the results in ascending order by default