The ORDER BY clause expresses the order in ascending or descending
order.
If we do not define ORDER BY in the statement, it is alphabetically by
default (ascending).
Structure:
SELECT column_name FROM table_name ORDER BY column_name ASC|DESC;
Example1:
We have a table Customer with this data:
CustomerID | Name | Hair | Salary |
---|---|---|---|
1 | Alechandro | brown | 100 |
2 | Katerina | blonde | 150 |
3 | Lucie | blonde | 30 |
4 | Thomas | black | 40 |
5 | Theodor | black | 120 |
6 | Thomassino | black | 99 |
7 | Knor | brown | 50 |
8 | Thor | blonde | 10 |
9 | Lucie | pink | 70 |
SELECT name FROM Customer ORDER BY name asc;
NAME |
---|
Alechandro |
Katerina |
Knor |
Lucie |
Lucie |
Theodor |
Thomas |
Thomassino |
Thor |
Example2:
SELECT name FROM Customer ORDER BY name desc;
NAME |
---|
Thor |
Thomassino |
Thomas |
Theodor |
Lucie |
Lucie |
Knor |
Katerina |
Alechandro |
Example3:
SELECT disctinct name FROM Customer ORDER BY name desc;
NAME |
---|
Thor |
Thomassino |
Thomas |
Theodor |
Lucie |
Knor |
Katerina |
Alechandro |
I give people clear, functional, and proven instructions. I gently guide them so that they can fulfill their IT dreams.