Sort the column in Postgresql order by – Ascending , Descending

In this Tutorial we will see how to sort the column in Postgresql using order by clause either in ascending or descending order  with example. Lets see

  • How to sort the column in Postgresql using order by clause in ascending order
  • How to sort the column in Postgresql using order by clause in descending order
  • Sorting multiple column in postgresql

With an example for each.

We will be using the table : books

Sort the column in Postgresql order by – Ascending , Descending 1

 

Sort the column in Posgresql ascending order :

Now let’s sort the books table by increasing order of price

SELECT * FROM books ORDER BY price

So the resultant sorted table is

Sort the column in Postgresql order by – Ascending , Descending 2

 

Sort the column in Posgresql descending order :

Now let’s sort the books table by decreasing order of price

SELECT * FROM books ORDER BY price DESC

So the resultant sorted table is

Sort the column in Postgresql order by – Ascending , Descending 3

 

Sort the column in Posgresql descending order :

Now let’s sort the books table by decreasing order of price

SELECT * FROM books ORDER BY price, total_pages DESC

So the resultant table is first sorted by price in increasing order and if the two prices are same then books with more page is shown first (page is sorted in decreasing order)

Sort the column in Postgresql order by – Ascending , Descending 4