In today’s article, we will learn how to sort data with the Order By command in PostgreSQL.
With the Order By command, this order can be sorted from smallest to largest or alphabetically.
General Usage is as follows.
1 |
SELECT Column1, Column2 FROM Table_Name ORDER BY Column1 asc,Column2 desc |
The ORDER BY command takes two parameters:
ASC: ASC command is the abbreviation of the word ASCENDIG.
With this parameter, we can sort textual expressions alphabetically from smallest to largest.
DESC: DESC command is the abbreviation of the word DESCENDIG.
With this parameter, we can sort textual expressions alphabetically from largest to smallest.
You can use asc and desc parameters in the table as follows.
ORDER BY columnName asc
To sort in more than one column:
ORDER BY column1 asc or desc, column2 asc or desc
Let’s reinforce it with an example.
First, let’s create our table.
1 |
create table ascdescexample(text text, column 1 integer) |
Let’s add our data to our ascdescörnek table.
1 2 3 4 5 6 7 8 9 10 |
insert into ascdescörnek values ('a',1); insert into ascdescörnek values ('b',2); insert into ascdescörnek values ('c',3); insert into ascdescörnek values ('d',4); insert into ascdescörnek values ('e',5); insert into ascdescörnek values ('f',6); insert into ascdescörnek values ('g',7); insert into ascdescörnek values ('ğ',8); insert into ascdescörnek values ('i',9); insert into ascdescörnek values ('ı',10); |
Let’s see how our data is sorted by sorting the textt column in our table as desc.
As seen above, we sorted the text column as desc, and our other column is affected by this and performs the sorting process as desc.
Let’s list the numerical value in column 1 as asc.